UILabel中心内容

Lin*_*int 7 iphone text uilabel ios

这是我的代码:

UILabel *myLabel;
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 480)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
myLabel.text = @"Please select at least one image category!";
myLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size: 24.0];
myLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
myLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:myLabel];
[myLabel release];
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以将包装好的文本居中,因为它是左侧的: 替代文字

aru*_*r.p 26

myLabel.textAlignment=UITextAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)

  • 适用于ios6的NSTextAlignmentCenter (9认同)

Jos*_*nce 13

UITextAlignmentCenter是使文本居中的正确方法,但现在已折旧.- 看到这个问题和答案.

NSTextAlignmentCenter改为使用:

label.textAlignment = NSTextAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)

在评论中提到了这一点,只是为了像我这样的新秀.