4

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

但是最后一行无法正常工作,尽管stringPhstrEditedText中。所有的文字都是蓝色的。

4

2 回答 2

3

我设置了一个示例,您可以在此处下载,我发现它运行良好。
和:

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

模拟器正确突出显示文本的右侧部分: 在此处输入图像描述

您绝对确定您的字符串stringPh是 的实际子字符串strEditedText吗?

于 2012-05-29T21:30:49.137 回答
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];
于 2012-05-31T07:23:06.240 回答