核心文本 - 从NSRange获取像素坐标 [英] Core Text - Get Pixel Coordinates from NSRange

查看:101
本文介绍了核心文本 - 从NSRange获取像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用Core Text呈现的文本,我如何从 NSRange 获得 CGRect



我正在使用带有 NSAttributedString 的核心文本。

解决方案

这是一个绝对的痛苦,但可以做到。



你需要使用获取帧中的所有行CTFrameGetLines(),使用 CTLineGetStringRange()检查它们的字符范围是否在您要查找的范围内,使用 CTLineGetTypographicBounds()查找行本身呈现的大小,并使用 CTLineGetOffsetForStringIndex()来确定起始/结束字符的实际位置范围(如果该线只是所需范围的子范围)。



结合所有这些并累加偏移量和高度等等可以得到你想要的。请注意, CTLineGetImageBounds()在没有图形上下文的情况下不起作用(而且,从我收集的内容来看,它无论如何都非常昂贵)并且没有必要解决这个问题。 / p>

How would I get a CGRect from an NSRange for text rendered with Core Text?

I am using Core Text with an NSAttributedString.

解决方案

It is an absolute pain but can be done.

You need to get all of the lines in the frame using CTFrameGetLines(), check if their character range is in the range you're looking for using CTLineGetStringRange(), use CTLineGetTypographicBounds() to find how big the line itself would render as, and use CTLineGetOffsetForStringIndex() to determine the actual position of the start/end character of the range (if the line is just a subrange of the desired range).

Combining all of this and adding up offsets and heights and such can get you what you want. Note that CTLineGetImageBounds() doesn't work without a graphics context (and, from what I gather, it is pretty expensive anyway) and is not necessary to solve this problem.

这篇关于核心文本 - 从NSRange获取像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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