Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding location of specific characters in UILabel on iPhone

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

Question: How do I find the coordinates of all letters "d" in this 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)

like image 590
Nick Avatar asked Jul 07 '10 00:07

Nick


1 Answers

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;
like image 153
drawnonward Avatar answered Oct 21 '22 18:10

drawnonward