简体   繁体   中英

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. 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.

Try this solution:

Here we get the size the entire 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM