我需要帮助获取子串的位置。我列举了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];
}
}];
}
答案 0 :(得分: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跨越多条线路会变得更加复杂......
答案 1 :(得分: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];
}
}