iOS 6 中的标签对齐 - UITextAlignment 已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11920321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:42:15  来源:igfitidea点击:

Label Alignment in iOS 6 - UITextAlignment deprecated

iosalignmentlabel

提问by user123

Seems like UITextAlignmentCenteris deprecated in iOS 6.

似乎UITextAlignmentCenter在 iOS 6 中已弃用。

I still use it and works well, but it gives a warning.

我仍在使用它并且运行良好,但它给出了警告。

How can I fix this?

我怎样才能解决这个问题?

label.textAlignment = UITextAlignmentCenter;

Thanks.

谢谢。

回答by majorl3oat

In iOS6 you can use

在 iOS6 中你可以使用

label.textAlignment = NSTextAlignmentCenter;

Hope this helps.

希望这可以帮助。

回答by Wienke

The labelAlignmentproperty change is probably related to Apple's introducing NSAttributedStrings to more of the iOS controls, and therefore needing to change the UIText… properties to NSText… properties.

labelAlignment属性变化可能与苹果的引入NSAttributedStrings更多iOS的控制,因此需要改变UIText ...属性NSText ...性质。

So if you've upgraded to iOS6, you're in clover; just switch from UITextAlignmentCenterto NSTextAlignmentCenterand enjoy the fancy new strings.

所以如果你已经升级到 iOS6,你就在三叶草中;只需从UITextAlignmentCenterto切换NSTextAlignmentCenter并享受花哨的新琴弦。

But if you're working with a complex project and would prefer that the earth not move so much under your feet, you might want to stick with an older version for a while, and adapt your code for multiple versions, something like this:

但是,如果您正在处理一个复杂的项目,并且希望地球不会在您的脚下移动太多,您可能希望坚持使用旧版本一段时间,并针对多个版本调整您的代码,如下所示:

// This won't compile:
if ([label respondsToSelector:@selector(attributedText:)]) 
    label.textAlignment = UITextAlignmentCenter;
else 
    label.textAlignment = NSTextAlignmentCenter;

The above approach works for new methods; you get warnings but everything runs fine. But when the compiler sees a constantthat it doesn't know about, it turns red and stops in its tracks. There's no way to sneak NSTextAlignmentCenterpast it. (Well, there might be a way to customize the compiler's behavior here, but it seems inadvisable.)

上述方法适用于新方法;您会收到警告,但一切正常。但是当编译器看到一个它不知道的常量时,它会变成红色并停止运行。没有办法偷偷NSTextAlignmentCenter过去。(好吧,这里可能有一种方法可以自定义编译器的行为,但似乎不可取。)

The workaround is to add some conditional preprocessor defines. If you put something like this in your class's h file (or perhaps in an imported constants file -- which must itself include #import <UIKit/UIKit.h>in order to ever know about the NSText... constants)…

解决方法是添加一些条件预处理器定义。如果你把这样的东西放在你班级的 h 文件中(或者可能在一个导入的常量文件中——它本身必须包含#import <UIKit/UIKit.h>才能知道 NSText... 常量)......

#ifdef NSTextAlignmentCenter // iOS6 and later
#   define kLabelAlignmentCenter    NSTextAlignmentCenter
#   define kLabelAlignmentLeft      NSTextAlignmentLeft
#   define kLabelAlignmentRight     NSTextAlignmentRight
#   define kLabelTruncationTail     NSLineBreakByTruncatingTail 
#   define kLabelTruncationMiddle   NSLineBreakByTruncatingMiddle
#else // older versions
#   define kLabelAlignmentCenter    UITextAlignmentCenter
#   define kLabelAlignmentLeft      UITextAlignmentLeft
#   define kLabelAlignmentRight     UITextAlignmentRight
#   define kLabelTruncationTail     UILineBreakModeTailTruncation
#   define kLabelTruncationMiddle   UILineBreakModeMiddleTruncation
#endif

…you can do this:

…你可以这样做:

label.textAlignment = kLabelAlignmentCenter;

And this:

和这个:

label.lineBreakMode = kLabelTruncationMiddle;

Etc.

等等。

Since these UIText/NSText changes are likely to be popping up for multiple controls, this approach is quite handy.

由于这些 UIText/NSText 更改可能会针对多个控件弹出,因此这种方法非常方便。

(Caveat: Being a member of the aforementioned steady-earth lovers, I have tested this with an old version, but not yet with iOS6.)

(警告:作为上述稳定地球爱好者的一员,我已经用旧版本对此进行了测试,但尚未在 iOS6 上进行测试。)

回答by nhisyam

NSTextAlignmentCentercan be used in place of UITextAlignmentCenterand a list of other replacements is below:

NSTextAlignmentCenter可以代替使用UITextAlignmentCenter,其他替换列表如下:

#ifdef __IPHONE_6_0 // iOS6 and later
#   define UITextAlignmentCenter    NSTextAlignmentCenter
#   define UITextAlignmentLeft      NSTextAlignmentLeft
#   define UITextAlignmentRight     NSTextAlignmentRight
#   define UILineBreakModeTailTruncation     NSLineBreakByTruncatingTail
#   define UILineBreakModeMiddleTruncation   NSLineBreakByTruncatingMiddle
#endif

回答by neoneye

#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
label.text = @"There is no spoon";
label.textAlignment = ALIGN_CENTER;
[self addSubview:label];

回答by Aaron Brager

You don't have to do either of these. Xcode 4.5 will compile the NSTextAlignmentCenter, etc. fine in iOS 5.

您不必执行其中任何一项。Xcode 4.5 将NSTextAlignmentCenter在 iOS 5 中编译等。

回答by Gaurav Gilani

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 40)];
[label1 setText:@"Your String"];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 sizeToFit];

//For Center Alignment
[label1 setTextAlignment:NSTextAlignmentCenter];

//For Right Alignment
[label1 setTextAlignment:NSTextAlignmentRight];

//For Left Alignment
[label1 setTextAlignment:NSTextAlignmentLeft];

// Add the label into the view

[self.view addSubview:label1];

回答by Mubin Shaikh

labelName.textAlignment=NSTextAlignmentLeft;

回答by BrainyMonkey

I had a similar issue and used the following: detailsLabel.textAlignment = NSTextAlignmentCenter;

我有一个类似的问题并使用了以下内容: detailsLabel.textAlignment = NSTextAlignmentCenter;

回答by Hiren Dhamecha

in iOS 6 or greater

在 iOS 6 或更高版本中

use this value :

使用这个值:

self.lbl_age.textAlignment=NSTextAlignmentCenter;

self.lbl_age.textAlignment=NSTextAlignmentCenter;

回答by Jerry Krinock

Swift 3:

斯威夫特 3:

label.textAlignment = NSTextAlignment.center