New NSAttributedString multiline

15,776

Solution 1

The lineBreakMode property isn't deprecated in iOS 6. It has simply changed the names of the constants. The old constants are deprecated, but still available. You can use the new constants even if you are deploying to an older iOS, because the constants are just enum values. The old names and the new names have the same values. So, just set memoLabel.lineBreakMode = NSLineBreakByTruncatingTail.

Your example code doesn't appear to take advantage of any attributed string specific features. If you don't need an attributed string, just keep using a plain string. That still works in iOS 6.

Solution 2

Use NSLineBreakByTruncatingTail instead of UILineBreakModeTailTruncation

Share:
15,776
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    I worked UILabel. But setLineBreakMode is deprecated. I have been using NSAttributedString. but UILabel setLineBreakMode is After that UILabel setNumberOfLines else does not work.

    Before:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
    label.text = @"XXXXXX";
    memoLabel.textAlignment = UITextAlignmentLeft;
    memoLabel.numberOfLines = 2;
    memoLabel.lineBreakMode = UILineBreakModeTailTruncation;
    memoLabel.font = [UIFont systemFontOfSize:11];
    memoLabel.backgroundColor = [UIColor clearColor];
    

    IOS 6 after:

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSAttributedString *string
        = [[NSAttributedString alloc] initWithString:text
                                      attributes:[NSDictionary
                                                  dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
                                                  NSFontAttributeName,
                                                  paragraphStyle, NSParagraphStyleAttributeName,nil]];
    [paragraphStyle release];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
    label.attributedText = string;
    [string relase];
    

    I want to be the same before and after the display. How to display multiple lines?