Reputation: 943
None of the methods listed below give the exact visible bounds of a text in Cocoa. How do we obtain that?
NSString
's boundingRectWithSize:options:attributes:
NSAttributedString
's boundingRectWithSize:options:
NSLayoutManager
's usedRectForTextContainer:
To illustrate this, here's a screenshot of the result of using the second method above as shown in this sample code snippet.
Upvotes: 5
Views: 1583
Reputation: 943
Here's the solution I coded up. Any suggestions for improvements are most welcome.
code snippet: http://pastebin.com/qJMKwvau
screenshot: http://ScrnSht.com/jskyqs
Upvotes: 1
Reputation: 8180
Actually, from the typographic point of view, the box is correct, but the font exceeds is own bound. You can try the option NSStringDrawingUsesDeviceMetrics
, that uses glyph bounds instead of typographic bounds.
Upvotes: 4