How to get height for NSAttributedString at a fixed width

后端 未结 12 1087
萌比男神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:29

    -[NSAttributedString boundingRectWithSize:options:]
    

    You can specify NSStringDrawingUsesDeviceMetrics to get union of all glyph bounds.

    Unlike -[NSAttributedString size], the returned NSRect represents the dimensions of the area that would change if the string is drawn.

    As @Bryan comments, boundingRectWithSize:options: is deprecated (not recommended) in OS X 10.11 and later. This is because string styling is now dynamic depending on the context.

    For OS X 10.11 and later, see Apple's Calculating Text Height developer documentation.

提交回复
热议问题