使用NSLayoutManager计算每个字形的帧 [英] Using NSLayoutManager to calculate frames for each glyph

查看:134
本文介绍了使用NSLayoutManager计算每个字形的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此主题中,核心文字在iOS中计算字母框 ,他们能够使用核心文本非常精确地计算每个字形的框架。最终的rects完美地拥抱了实际绘制的字形。

On this thread, Core Text calculate letter frame in iOS, they were able to calculate the frame of each glyph very precisely using Core Text. The final rects hug the actual drawn glyphs perfectly.

使用NSLayoutManager的 boundingRectForGlyphRange:inTextContainer:似乎没有返回精确的字形边界框:

Using NSLayoutManager's boundingRectForGlyphRange:inTextContainer: doesn't seem to return the glyph bounding boxes that precisely:

并且返回的rects没有完全包含更复杂的字体(Zapfino示例):

And the returned rects don't fully enclose more complex fonts (Zapfino example):

任何人都知道如何复制上述讨论的结果而不进入仅限iOS 7的应用程序的核心文本?

Does anyone know how to replicate the results from the above mentioned discussion without going into Core Text under iOS 7 only apps?

谢谢。

推荐答案

我找到了这个问题的解决方案,或者至少是

I've found a solution for this issue or at least sort of

问题: boundingRectForGlyphRange RTL文本的结果是关闭的。

Problem: boundingRectForGlyphRange results is off in case of RTL text.

因此,仅在检测到RTL文本的情况下:

So in case of RTL text is detected only:

使用 NSLayoutManager 方法 locationForGlyphAtIndex ,对于范围内的每个字母。这些是范围内每个字形的起点。
使用这些点我正确调整boundingRect。

Using NSLayoutManager method locationForGlyphAtIndex, for each letter in the range. These are the starting points of each glyph in the range. Using these points I adjust the boundingRect correctly.

以下是它的概述:

CGPoint endPoint = ((NSValue *)pointsOfGlyps.firstObject).CGPointValue;
CGPoint startPoint = ((NSValue *)pointsOfGlyps.lastObject).CGPointValue;
boundingRect.origin.x =  startPoint.x;
boundingRect.size.width = endPoint.x - startPoint.x + letterWidth;

letterWidth 是字母宽度的近似值计算为两个连续起点之间的差值。

letterWidth is an approximation of a letter width calculated as the delta between two consecutive starting points.

这篇关于使用NSLayoutManager计算每个字形的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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