[英]Find width of attributed text in a UILabel when the text has multiple lines
I have multiline attributed text in a UILabel that I would like to find the width of. 我想在UILabel中找到多行属性文本的宽度。 The purpose of this is to resize the font size for different devices.
这样做的目的是为了调整不同设备的字体大小。 To do this I check whether the text with line breaks will fit a given space.
为此,我检查带有换行符的文本是否适合给定的空格。 I have tried
text.size.width
, however this will give the width as if the text is taking up one line. 我已经尝试过
text.size.width
,但是这将给出宽度,就好像文本占用了一行。
Try this solution: 试试这个解决方案:
Here we get the size the entire NSAttributedString
这里我们得到整个
NSAttributedString
的大小
CGRect paragraphRect =
[attributedText boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
You can then use it to find the width of the NSAttributedString Hope this helps you out. 然后,您可以使用它来查找NSAttributedString的宽度,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.