简体   繁体   中英

get position of enumerated substring of UILabel

I need a help with getting position of substring. I have enumerated substrings of UILabel, one randomly is changed to "aaaaa" and color to clear. I want to put UIImage in x,y of that string. I can easy get width and height of that string but I can't get origin. Here is my code:

  if (cloudWord !=nil)
    {
        [label.text enumerateSubstringsInRange:NSMakeRange(0, [label.text length]) options:NSStringEnumerationByWords usingBlock:^(NSString* word, NSRange wordRange, NSRange enclosingRange, BOOL* stop)
         {
             if ([word isEqualToString:@"aaaaa"])
             {
                 NSLog(@"cloud");
                 [labelText addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:wordRange];
                 [label setAttributedText:labelText];

                 UIImageView *cloudImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"playBtn.png"]];
                 [cloudImageView setFrame:CGRectMake(labelText.size.width, labelText.size.height, 40, 40)];

                 [self addSubview:cloudImageView];

             }
         }];
    }

First find the range of your substring:

NSRange range = [myString rangeOfString:subString];

Then take the substring that precedes that range (from index 0 through the index before the first character of your substring) and find the width of that substring when rendered in the UILabel's font:

NSString *prefix = [myString substringToIndex:range.location];
CGSize size = [prefix sizeWithFont:[UIFont systemFontOfSize:fontSize]];
CGPoint p = CGPointMake(size.width, 0);

This will leave you with p as the (x,y) coordinate of the upper left corner of the substring within your UILabel. You may need to convert that to the coordinate system of your label's superview, depending on how you plan to display the image.

This assumes it's a 1-line UILabel. It gets much more complicated if your UILabel spans multiple lines...

If I am right You want to know the location of substring in your string, for that you can use this

NSRange range = [myString rangeOfString:subString options:NSBackwardsSearch range:yourRange];
NSLog(@"%u", range.loaction);

UPDATED:

You have to handle the cases for more than one line..

 -(void)method
    {
     NSString *replaceString = @"aaaaa";
    CGSize maximumLabelSize = CGSizeMake(200, FLT_MAX);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 20)];
    NSString * string= @"1236587465987997970957986985090fhyjkhkk aaaaa 687439";
    CGSize expectedLabelSize = [string sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    label.text = string;
    label.backgroundColor = [UIColor redColor];

    label.numberOfLines = 3;
    [self.view addSubview:label];
    NSRange range = [label.text rangeOfString:replaceString options:NSBackwardsSearch range:NSMakeRange(0, label.text.length)];
       NSString *substring = [label.text substringWithRange:NSMakeRange(0, range.location)];
    float stringWidth = [substring sizeWithFont:label.font].width;
    float count =0;
    float imgWidth = [replaceString sizeWithFont:label.font].width;


        while( stringWidth> label.frame.size.width-5)
        {
            stringWidth  = stringWidth - label.frame.size.width;
            count++;
        }
        float imgX = stringWidth + label.frame.origin.x;

    float imgHeight = 0;
    float imgY = 0;
    if (count >0) {
        imgY = ((label.frame.size.height/(count+1))*count)+ label.frame.origin.y-10*count;

    }
    else
    {
        imgY = label.frame.origin.y;
        imgHeight = [substring sizeWithFont:label.font].height;
    if(stringWidth + imgWidth > label.frame.size.width)
    {
        imgX = label.frame.origin.x;
        imgY = imgY + (label.frame.size.height/(count+1)) -10;
    }


    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imgX,imgY , imgWidth, imgHeight)];
    imgView.image = [UIImage imageNamed:@"btn_Links.png"];
    [self.view addSubview:imgView];
    }

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM