【问题标题】:UILabel Center contentsUILabel 中心内容
【发布时间】:2011-05-26 08:38:01
【问题描述】:

这是我的代码:

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];

我想知道是否可以将换行的文本居中,因为它与左侧对齐:

【问题讨论】:

    标签: iphone ios text uilabel


    【解决方案1】:
    myLabel.textAlignment=UITextAlignmentCenter;
    

    【讨论】:

    • NSTextAlignmentCenter for ios6
    【解决方案2】:

    UITextAlignmentCenter 是使文本居中的正确方法,但现在已弃用。 - see this question and answer.

    改用NSTextAlignmentCenter

    label.textAlignment = NSTextAlignmentCenter;
    

    这是在评论中提到的,只是为了让像我这样的新手清楚。

    【讨论】:

      【解决方案3】:

      使用这个:

      myLabel.textAlignment = NSTextAlignmentCenter;
      

      UITextAlignmentCenter 已从 iOS 6 中弃用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 2011-07-03
        • 2010-10-27
        • 2018-01-17
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多