How to add line break for UILabel?

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

    If you set your UILable properties from Plain to Attributed...the UILabel will hold multiline text no matter how many paragraphs for along as your UILabel height and width are set to fit the screen area you want to display the text in.

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

    In the interface builder, you can use Ctrl + Enter to insert /n to the position you want. This way could implement the following situation

    aaa
    aaaaaaa

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

    If you read a string from an XML file, the line break \n in this string will not work in UILabel text. The \n is not parsed to a line break.

    Here is a little trick to solve this issue:

    // correct next line \n in string from XML file
    NSString *myNewLineStr = @"\n";
    myLabelText = [myLabelText stringByReplacingOccurrencesOfString:@"\\n" withString:myNewLineStr];
    
    myLabel.text = myLabelText;
    

    So you have to replace the unparsed \n part in your string by a parsed \n in a hardcoded NSString.

    Here are my other label settings:

    myLabel.numberOfLines = 0;
    myLabel.backgroundColor = [UIColor lightGrayColor];
    myLabel.textColor = [UIColor redColor]; 
    myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
    myLabel.textAlignment = UITextAlignmentCenter;
    

    Most important is to set numberOfLines to 0 (= unlimited number of lines in label).

    No idea why Apple has chosen to not parse \n in strings read from XML?

    Hope this helps.

    0 讨论(0)
  • 2020-11-29 15:17
    NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
    NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"];
    
    0 讨论(0)
  • 2020-11-29 15:21

    Use \n as you are using in your string.

    Set numberOfLines to 0 to allow for any number of lines.

    label.numberOfLines = 0;

    Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.

    UILabel *label; // set frame to largest size you want
    ...
    CGSize labelSize = [label.text sizeWithFont:label.font
                              constrainedToSize:label.frame.size
                                  lineBreakMode:label.lineBreakMode];
    label.frame = CGRectMake(
        label.frame.origin.x, label.frame.origin.y, 
        label.frame.size.width, labelSize.height);
    

    Update : Replacement for deprecated

    sizeWithFont:constrainedToSize:lineBreakMode:

    Reference, Replacement for deprecated sizeWithFont: in iOS 7?

    CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
    
    label.frame = CGRectMake(
        label.frame.origin.x, label.frame.origin.y, 
        label.frame.size.width, labelSize.height);
    
    0 讨论(0)
  • 2020-11-29 15:21
    textLabel.text = @"\nAAAAA\nBBBBB\nCCCCC";
    textLabel.numberOfLines = 3; \\As you want - AAAAA\nBBBBB\nCCCCC
    textLabel.lineBreakMode = UILineBreakModeWordWrap;
    NSLog(@"The textLabel text is - %@",textLabel.text);
    
    0 讨论(0)
提交回复
热议问题