0

I have a label with a sentence in it, that is one string. I need to get the x coordinate for a specific word in the string. So for example, if I have a sentence that says "The dog ran" inside of the sentence, I need to be able to find the x and y coordinates, as well as the width and height to place a UITextField over it. Here is my code so far:

- (void)insertTextFields:(NSString *)string inLabel:(UILabel *)label { CGFloat stringWidth = [self getWidthOfString:string inLabel:label]; CGFloat stringHeight = label.bounds.size.height; CGFloat stringYOrigin = label.bounds.origin.y; CGFloat stringXOrigin = [self getXOriginOfString:string fromString:label.text inLabel:label]; CGRect textFieldRect = CGRectMake(stringXOrigin, stringYOrigin, stringWidth, stringHeight); UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect]; [label addSubview:textField]; } - (CGFloat)getWidthOfString:(NSString *)string inLabel:(UITextField *)label { CGFloat maxWidth = CGRectGetMaxX(label.frame); CGSize stringSize = [string sizeWithFont:label.font forWidth:maxWidth lineBreakMode:NSLineBreakByCharWrapping]; CGFloat width = stringSize.width; return width; } - (CGFloat)getXOriginOfString:(NSString *)string fromString:(NSString *)sentenceString inLabel:(UILabel *)label { CGFloat maxWidth = CGRectGetMaxX(label.frame); CGSize sentenceStringSize = [sentenceString sizeWithFont:label.font forWidth:maxWidth lineBreakMode:NSLineBreakByWordWrapping]; CGSize stringSize = [string sizeWithFont:label.font forWidth:maxWidth lineBreakMode:NSLineBreakByWordWrapping]; //I have the width of both the individual word and the sentence //now I need to find the X coordinate for the individual word inside of the sentence string return xOrigin; } 

Could someone tell me what I need to do to fix this?

1 Answer 1

1

Is that the x position of the start of the word? Just measure the string that precedes the word....

- (CGFloat)getXOriginOfString:(NSString *)string fromString:(NSString *)sentenceString inLabel:(UILabel *)label { CGFloat maxWidth = CGRectGetMaxX(label.frame); NSRange range = [sentenceString rangeOfString:string]; NSString *prefix = [sentenceString substringToIndex:range.location]; return [prefix sizeWithFont:label.font forWidth:maxWidth lineBreakMode:NSLineBreakByWordWrapping].width; } 

The end of the word will be this result + the stringSize in the code you posted. Or maybe I'm misunderstanding the question?

Sign up to request clarification or add additional context in comments.

6 Comments

Calculating glyph positions is a complex task. A typesetter might decide to place the start of the word at a different position when seeing the whole string. This approach might work in some situations but break in others.
I made some slight modifications to your code and it is working! Thanks fot the help.
Glad to hear. What did you change? ... so future readers can be helped.
NSString *prefix = [string substringToIndex:range.location]; - change here string to sentenceString cause your code is wrong now
@DmitriyKalachniuk - good catch. Edited. Thanks for improving it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.