UILabel获取文本子字符串的CGRect [英] UILabel get CGRect for substring of text

查看:98
本文介绍了UILabel获取文本子字符串的CGRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标很简单:从UILabel获取标签文本中子字符串的矩形。从广泛的搜索来看,似乎没有内置任何东西来处理这个问题。其他人也提出了类似的问题,但没有人在Stackoverflow上完全回答过。

My objective is simple: From a UILabel get the rect of a substring in the text of the label. From extensive searching there doesn't appear to be anything built in to handle this. Other people have asked similar questions, but none have been answered fully here on Stackoverflow.

// Something like this perhaps (added as a category)
CGRect rect = [myLabel rectForRange:NSMakeRange(3, 5)];

它可以用于什么的一个例子(只是为了澄清我正在搜索的内容):

An example of what it could be used for (just to clarify what I'm searching for):

推荐答案

这对于一个角色来说几乎是必需的,所以我疯了,写了一些代码,可以准确地计算出最基本的UILabel的矩形。标准的UILineBreakMode和文本对齐。

This was mostly needed for one character rects, so I went crazy and wrote some code that could accurately calculate the rect for the most basic UILabel. Standard UILineBreakMode and text alignment.

我希望如果我将它发布给公众,那么代码会对它做出贡献并改进它,特别是因为我不知道这么多关于文本呈现!

I was hoping that if I release it to the public people code contribute to it and improve it, especially since I don't know so much about text rendering!

代码:

https://gist.github.com/1278483

这篇关于UILabel获取文本子字符串的CGRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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