NSMutableAttributedString的自动换行 [英] Word wrap for NSMutableAttributedString

查看:2324
本文介绍了NSMutableAttributedString的自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NSMutableAttributedString,字符串非常长。我想在UIlabel上显示它时进行自动换行。如果它是NSString,我会继续这样做,
动态UILabel截断文本
但我怎么能用NSAttributedString呢?
一旦完成,我需要根据标签大小调整视图大小。

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.

推荐答案

lineBreakMode属性在iOS 6中不推荐使用。它只是更改了常量的名称。旧常量已弃用,但仍可用。即使您要部署到较旧的iOS,也可以使用新常量,因为常量只是枚举值。旧名称和新名称具有相同的值。所以,只需设置yourlabelname.lineBreakMode = NSLineBreakByTruncatingTail。

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])];

这篇关于NSMutableAttributedString的自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆