在iPhone上查找UILabel中的特定字符的位置 [英] finding location of specific characters in UILabel on iPhone

查看:712
本文介绍了在iPhone上查找UILabel中的特定字符的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel一些文本,说Hello World abcdefg标签可以有多行,不同的字体大小等。

I have a UILabel with some text, say "Hello World abcdefg" The label can have multiple lines, different font sizes etc.

em>:我如何找到这个UILabel中所有字母d的坐标。

Question: How do I find the coordinates of all letters "d" in this UILabel.

逻辑第一步是找到字符串中这些字符的位置(UILabel。文本),但是如何将它转换为坐标,当它实际上绘制在屏幕上

Logical first step is find the position of those characters in the string (UILabel.text), but then how do I translate that into coordinates when it's actually drawn on screen

这个想法是找到这些坐标,并在该字符上绘制自定义的东西

The idea is to find those coordinates and draw something custom on top of that character (basically to cover it with a custom image)

推荐答案

在iPhone上测量文本的基本工具在 UIStringDrawing.h ,但没有人做你所需要的。你基本上必须遍历子字符一次一个字符测量每个。当一个行换行时(结果更高),在最后一个没有换行的字符之后分割,并将行高添加到y坐标。

The basic tools for measuring text on iPhone are in UIStringDrawing.h but none of them do what you need. You will basically have to iterate through substrings one character at a time measuring each. When a line wraps (the result is taller), split after the last character that did not wrap and add the line height to your y coordinate.

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode;

这篇关于在iPhone上查找UILabel中的特定字符的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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