NSAttributedString忽略UILabel的自动收缩和numberOfLines(iOS 6) [英] NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)

查看:545
本文介绍了NSAttributedString忽略UILabel的自动收缩和numberOfLines(iOS 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UILabel与
行数= 2
系统字体大小= 15
最小字体大小= 8
换行模式 - 截断尾部

I have UILabel with number of lines = 2 system font size = 15 minimum font size = 8 Line break mode - Truncate tail

当我为UILabel设置具有NSString类型的长文本时,它可以正常工作并显示多行文本(如果需要,可以缩放)。
当我尝试使用NSAttributedString类型设置文本时,它忽略了最小字体大小和自动收缩,因此我看到一个最大字体大小的行文本。

When I set long text which have type NSString for UILabel it works fine and shows multiline text (scaled if needed). When I am trying to set text with type NSAttributedString it ignores minimum font size and Autoshrink so I see one line text with maximum font size.

是否可能解决这个问题

看起来像这样(标签大小是常量)

Looks something like this (Label size is const)

-----------------------
| normal NSString  Text|
| very  very  long ... | 
-----------------------

---------------------------
|NSAttributedString tex...|
---------------------------


推荐答案

我找到了一种方法:

label.adjustsFontSizeToFitWidth = true
label.attributedText = attributedString
label.lineBreakMode = .ByTruncatingTail // this did the trick!

仅在设置属性字符串后设置第三行时才有效。设置(以及其他内容)时,属性字符串似乎会覆盖换行符。

It only works if the third line is set after setting the attributed string. It seems like the attributed string overrides line break behavior when set (among other things).

这篇关于NSAttributedString忽略UILabel的自动收缩和numberOfLines(iOS 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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