Finding text's bounding rect in Core Text

我怕爱的太早我们不能终老 提交于 2019-12-10 04:08:12

问题


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?

Update:

To make sure it's not a problem related to the font, I used different fonts. The problem exists even for ASCII characters in Arial Italic.

Update:

Ignore my previous update! It turns out this one (problem with Arial Italic) was my own bug. The first problem still stands though.


回答1:


Following the bug report I made, apparently this is a CoreText problem, appearing in rare cases. Will let you know as soon as I find a fix.

Update: March 15 '12

I forgot to update this sooner, but this is fixed. The fix is in OS X Lion / iOS 5.




回答2:


As far as I can tell, CTLineGetImageBounds is the correct function to use. I suggest filing a bug report in RadarWeb.



来源:https://stackoverflow.com/questions/2770252/finding-texts-bounding-rect-in-core-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!