Label Alignment in iOS 6 - UITextAlignment deprecated

前端 未结 11 768
再見小時候
再見小時候 2020-12-12 10:59

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?

<         


        
相关标签:
11条回答
  • 2020-12-12 11:22

    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
    
    0 讨论(0)
  • 2020-12-12 11:28

    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 
    }
    
    0 讨论(0)
  • 2020-12-12 11:33
    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];
    
    0 讨论(0)
  • 2020-12-12 11:33

    in iOS 6 or greater

    use this value :

    self.lbl_age.textAlignment=NSTextAlignmentCenter;

    0 讨论(0)
  • 2020-12-12 11:33

    Swift 3:

    label.textAlignment = NSTextAlignment.center
    
    0 讨论(0)
提交回复
热议问题