在核心文本中查找文本的边界rect [英] Finding text's bounding rect in Core Text

查看:126
本文介绍了在核心文本中查找文本的边界rect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Core Text中找到一行文本的边界。为了简单起见,假设它具有单个字符。
现在我使用以下方法:

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

它在大多数时候都可以工作,但对于一些字符,如math italic d(Unicode:0x1D451)或数学斜体q(Unicode:0x1D45E),宽度有点短。



我尝试使用 CTLineGetTypographicBounds() CTFramesetterSuggestFrameSizeWithConstraints ,但他们没有帮助(我认为他们使用glyph的advance来找到宽度,而不是它的图形宽度。)因为字体本身不是斜体,我也不能使用斜角来纠正这一点。



我试图直接访问字形并使用 CTFontCreatePathForGlyph(),但失败,因为CGGlyph和UniChar都是16位,我需要32位字符。



有人知道我做错了什么吗?如果是,正确的方法是什么?



更新:



它不是一个与字体相关的问题,我使用了不同的字体。



更新:



忽略我以前的更新!原来这一个(Arial Italic的问题)是我自己的错误。第一个问题仍然存在。

解决方案

按照我做的错误报告,显然这是一个CoreText问题,出现在罕见的情况。


$ b

更新日期:2012年3月15日 $ b

我忘记更快地更新,但这是固定的。该修复程序位于OS X Lion / iOS 5中。


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.

解决方案

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.

这篇关于在核心文本中查找文本的边界rect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆