NIAttributedLabel settextcolor无效

时间:2012-05-24 11:00:07

标签: iphone ios nimbus-ios

我正在使用NIAttributedLabel来显示文字链接。

            NIAttributedLabel *label;
            label = [[NIAttributedLabel alloc] initWithFrame:rect];

            label.delegate = self;
            label.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE];
            label.textAlignment = UITextAlignmentLeft;
            label.lineBreakMode = UILineBreakModeWordWrap;
            label.numberOfLines = 0;
            label.backgroundColor = [UIColor clearColor];
            label.highlightedTextColor = [UIColor whiteColor];  
            label.text = strEditedText;
            label.textColor = [UIColor blackColor];

            [label setTextColor:[UIColor blueColor] 
                          range:[strEditedText rangeOfString:stringPh]];  

但是,尽管 stringPh 位于 strEditedText 中,但最后一行无效。所有文字都是蓝色的。

2 个答案:

答案 0 :(得分:3)

我设置了一个可以下载here的示例,我发现它完美无缺 用:

NSString * strEditedText= @"I'm text";
NSString *stringPh = @"I'm";

模拟器正确突出显示文本的右侧部分: enter image description here

您是否绝对确定字符串stringPhstrEditedText的实际子字符串?

答案 1 :(得分:2)

这是我的工作代码

NSString * strEditedText= @"Please contact abc at 800.493.0016, option #3 for further assistance.";
NSString *stringPh = @"800.493.0016";
NIAttributedLabel *label;
label = [[NIAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

label.delegate = self;
label.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE];
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];
label.highlightedTextColor = [UIColor whiteColor];  
label.text = strEditedText;
label.textColor = [UIColor blackColor];

[label setTextColor:[UIColor blueColor] 
              range:[strEditedText rangeOfString:stringPh]];  
[self.view addSubview:label];