Finding text's bounding rect in Core Text

Posted by Mo on Stack Overflow See other posts from Stack Overflow or by Mo
Published on 2010-05-05T02:25:11Z Indexed on 2010/05/05 2:28 UTC
Read the original article Hit count: 310

Filed under:
|
|
|

I'm trying to find the boundaries of a line of text in Core Text. For simplicity, assume it has a single character. At the moment I'm using the following method:

line = CTLineCreateWithAttributedString(attrString);
rect = CTLineGetImageBounds(line, context);

It works most of the times, but for some characters, like math italic d (Unicode: 0x1D451) or math italic q (Unicode: 0x1D45E), the width is a bit short.

I tried using CTLineGetTypographicBounds() or CTFramesetterSuggestFrameSizeWithConstraints, but they didn't help either (I think they use glyph's advance to find the width, not its graphical width.) As the font itself isn't italic, I also can't use slant angle to correct this.

I tried accessing the glyphs directly and using CTFontCreatePathForGlyph(), but failed as CGGlyph and UniChar are both 16-bits and I need 32-bit characters.

Does anyone know if I'm doing anything wrong? If so, what's the right way?

© Stack Overflow or respective owner

Related posts about core-text

Related posts about cocoa