How to add line break for UILabel?

后端 未结 21 2068
失恋的感觉
失恋的感觉 2020-11-29 14:53

Let see that I have a string look like this:

NSString *longStr = @\"AAAAA\\nBBBBB\\nCCCCC\";  

How do I make it so that the UILabel disp

相关标签:
21条回答
  • 2020-11-29 15:28

    I have faced same problem, and here is, how i solved the problem. Hope this will be helpful for someone.

    // Swift 2

       lblMultiline.lineBreakMode = .ByWordWrapping // or use NSLineBreakMode.ByWordWrapping
       lblMultiline.numberOfLines = 0 
    

    // Objective-C

      lblMultiline.lineBreakMode = NSLineBreakByWordWrapping;
      lblMultiline.numberOfLines = 0;
    

    // C# (Xamarin.iOS)

      lblMultiline.LineBreakMode = UILineBreakMode.WordWrap;
      lblMultiline.Lines = 0;  
    
    0 讨论(0)
  • 2020-11-29 15:31

    For those of you who want an easy solution, do the following in the text Label input box in Interface Builder:

    Make sure your number of lines is set to 0.

    Alt + Enter

    (Alt is your option key)

    Cheers!

    0 讨论(0)
  • 2020-11-29 15:31

    On Xcode 6, you can just use \n even inside a string when using word wrap. It will work. So for example:

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, screenRect.size.width, 50)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"This will be on the first line\nfollowed by a line under it.";
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    
    0 讨论(0)
  • 2020-11-29 15:32

    You have to set the numberOfLines property on the UILabel. The default is 1, if you set it to 0 it will remove all limits.

    0 讨论(0)
  • 2020-11-29 15:32

    Important to note it's \n (backslash) rather than /n.

    0 讨论(0)
  • 2020-11-29 15:33

    In xCode 11, Swift 5 the \n works fine, try the below code:

    textlabel.numberOfLines = 0
    textlabel.text = "This is line one \n This is line two \n This is line three"
    
    0 讨论(0)
提交回复
热议问题