How to add line break for UILabel?

后端 未结 21 2070
失恋的感觉
失恋的感觉 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:34

    In Swift 2.2, > iOS 8

    I've set Lines = 0 on Storyboard, under Attribute Inspector and linked a referencing outlet to the label. Then use in controller like this:

     @IBOutlet weak var listLabel: UILabel!
    
     override func viewDidLoad() {
          ...
          listLabel.text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8"
     }
    
    0 讨论(0)
  • 2020-11-29 15:35

    Just using label.numberOfLines = 0;

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

    Just do it like this

    NSString * strCheck = @"A\nB";
    
    strCheck = [strCheck stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];  //This is to prevent for fetching string from plist or data structure
    
    label.numberOfLines = 0;
    
    label.lineBreakMode = NSLineBreakByWordWrapping;
    
    label.text = strCheck;
    
    0 讨论(0)
提交回复
热议问题