I have NSMutableAttributedString and the string is pretty long. I would like to do word wrap while displaying it on the UIlabel. If it was NSString, i will go ahead and do something like this, Dynamic UILabel truncating the text But how can i do it with NSAttributedString ? And once it is done, i need to resize the view depending on the label size.
Asked
Active
Viewed 1.2k times
3 Answers
9
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 yourlabelname.lineBreakMode = NSLineBreakByTruncatingTail.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0,[attributedStr length])];

python
- 1,407
- 1
- 15
- 31
-
1The OP asked for word wrapping, yet the voted reply truncates. Shouldn't this be `NSLineBreakByWordWrapping`? – strangetimes Oct 30 '21 at 09:43
5
Following also works irrespective of using attributedText
or normal text. Make sure to add the below line after setting the AttributedText
and font to the label.
label.lineBreakMode = .byTruncatingTail

Marián Černý
- 15,096
- 4
- 70
- 83

Nishchith
- 423
- 5
- 6
0
Set the following property after updating the attributedText field:
titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

Amol Dumrewal
- 1
- 2