0

我需要帮助来获取子字符串的位置。我已经枚举了 UILabel 的子字符串,其中一个随机更改为“aaaaa”,颜色为清除。我想把 UIImage 放在那个字符串的 x,y 中。我可以轻松获得该字符串的宽度和高度,但我无法获得原点。这是我的代码:

  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];

             }
         }];
    }
4

2 回答 2

1

首先找到子字符串的范围:

NSRange range = [myString rangeOfString:subString];

然后取该范围之前的子字符串(从索引 0 到子字符串第一个字符之前的索引),并在以 UILabel 的字体呈现时找到该子字符串的宽度:

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

这将使您将 p 作为 UILabel 中子字符串左上角的 (x,y) 坐标。您可能需要将其转换为标签超级视图的坐标系,具体取决于您计划如何显示图像。

这假设它是一个 1 行 UILabel。如果您的 UILabel 跨越多行,它会变得更加复杂......

于 2013-08-12T17:47:50.190 回答
0

如果我是对的你想知道你的字符串中子字符串的位置,你可以使用这个

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

更新:

您必须处理不止一行的案例。

 -(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];
    }

  }
于 2013-08-12T13:10:33.233 回答