Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

objective c - How do we measure visible bounds of text in Cocoa?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...