How to get height for NSAttributedString at a fixed width

后端 未结 12 1094
萌比男神i
萌比男神i 2020-12-12 18:53

I want to do some drawing of NSAttributedStrings in fixed-width boxes, but am having trouble calculating the right height they\'ll take up when drawn. So far, I\'ve tried:

12条回答
  •  旧巷少年郎
    2020-12-12 19:52

    Not a single answer on this page worked for me, nor did that ancient old Objective-C code from Apple's documentation. What I finally did get to work for a UITextView is first setting its text or attributedText property on it and then calculating the size needed like this:

    let size = textView.sizeThatFits(CGSize(width: maxWidth, height: CGFloat.max))
    

    Works perfectly. Booyah!

提交回复
热议问题