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?

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45

1 Answers1

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?

danh
  • 62,181
  • 10
  • 95
  • 136
  • 2
    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. – Nikolai Ruhe Apr 16 '13 at 17:37
  • I made some slight modifications to your code and it is working! Thanks fot the help. – Chandler De Angelis Apr 16 '13 at 17:51
  • Glad to hear. What did you change? ... so future readers can be helped. – danh Apr 16 '13 at 17:53
  • 1
    NSString *prefix = [string substringToIndex:range.location]; - change here string to sentenceString cause your code is wrong now – Dmitriy Kalachniuk Sep 17 '14 at 08:21
  • @DmitriyKalachniuk - good catch. Edited. Thanks for improving it. – danh Sep 17 '14 at 14:31
  • `sizeWithFont:forWidth:lineBreakMode:` is deprecated: first deprecated in iOS 7.0 - Use `-boundingRectWithSize:options:attributes:context:` – Iulian Onofrei Mar 26 '15 at 08:28