thisiscrazy4
thisiscrazy4

Reputation: 1965

Truncate part of string in NSAttributedString

I have 2 NSAttributedString's that I append together and then set it to be the text in the UILabel pictured. I want the first attributed string to truncate if it's going to be longer than 2 lines so that the second attributed string still shows up. How can I do this?

enter image description here

Upvotes: 3

Views: 3142

Answers (1)

Rafał Augustyniak
Rafał Augustyniak

Reputation: 2031

I think the easiest way will be to create two separate labels to display this text. The first one will display bolded text and the second one rest of the text. You will have to set NSLineBreakMode for the first attributed string as in example below:

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];


NSDictionary *attributes = @{/*yourattributes */NSParagraphStyleAttributeName: style};
NSAttributedString = [[NSAttributedString alloc] initWithString:YOUR_TEXT attributes:attributes];

You can even resign from using NSAttributedString in case you split your text into two separates labels. Instead you can use UILabel method to customize text appearance.

 label.font = font;
 label.textColor = [UIColor grayColor];

Upvotes: 1

Related Questions