How do we measure the visible borders of text in Cocoa?

None of the methods listed below give exact visible text boundaries in Cocoa. How do we get it?

  • NSString boundingRectWithSize:options:attributes:
  • NSAttributedString boundingRectWithSize:options:
  • NSLayoutManager usedRectForTextContainer:

To illustrate this, here is a screenshot of the result of using the second method above, as shown in this code snippet example .

+4
text objective-c fonts cocoa macos
source share
2 answers

Here's the solution I have encoded. Any suggestions for improvement are welcome. code snippet: http://pastebin.com/qJMKwvau
Screenshot: http://ScrnSht.com/jskyqs

+1
source share

Actually, from a typographic point of view, the field is correct, but the font exceeds its own border. You can try the NSStringDrawingUsesDeviceMetrics option, which uses glyph borders instead of typographic borders.

+4
source share

All Articles