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?