Seems like UITextAlignmentCenter
is deprecated in iOS 6.
I still use it and works well, but it gives a warning.
How can I fix this?
<
NSTextAlignmentCenter
can be used in place of UITextAlignmentCenter
and a list of other replacements is below:
#ifdef __IPHONE_6_0 // iOS6 and later
# define UITextAlignmentCenter NSTextAlignmentCenter
# define UITextAlignmentLeft NSTextAlignmentLeft
# define UITextAlignmentRight NSTextAlignmentRight
# define UILineBreakModeTailTruncation NSLineBreakByTruncatingTail
# define UILineBreakModeMiddleTruncation NSLineBreakByTruncatingMiddle
#endif
For Swift 5+ use:
yourLabel.textAlignment = .center
where You can set:
public enum NSTextAlignment : Int {
case left = 0 // Visually left aligned
case center = 1 // Visually centered
case right = 2 // Visually right aligned
case justified = 3 // Fully-justified. The last line in a paragraph is natural-aligned.
case natural = 4 // Indicates the default alignment for script
}
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];
in iOS 6 or greater
use this value :
self.lbl_age.textAlignment=NSTextAlignmentCenter;
Swift 3:
label.textAlignment = NSTextAlignment.center