Word wrap for NSMutableAttributedString

不打扰是莪最后的温柔 提交于 2019-12-21 09:12:16

问题


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.


回答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 yourlabelname.lineBreakMode = NSLineBreakByTruncatingTail.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr addAttribute:NSParagraphStyleAttributeName
                     value:paragraphStyle
                     range:NSMakeRange(0,[attributedStr length])];



回答2:


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


来源:https://stackoverflow.com/questions/12080942/word-wrap-for-nsmutableattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!