New NSAttributedString Multiline

I worked at UILabel. But setLineBreakMode is deprecated. I am using NSAttributedString. but UILabel setLineBreakMode 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?

+7
source share
2 answers

The lineBreakMode property lineBreakMode not deprecated in iOS 6. It just changed the names of the constants. Old constants are deprecated, but still available. You can use new constants even if you are using older iOS, because constants are just enumeration values. Old names and new names have the same meaning. So just set memoLabel.lineBreakMode = NSLineBreakByTruncatingTail .

Your code example does not use any string attributes. If you don't need a string with attributes, just keep using a regular string. This still works in iOS 6.

+4
source

Use NSLineBreakByTruncatingTail instead of UILineBreakModeTailTruncation

+2
source

All Articles