【问题标题】:NIAttributedLabel settextcolor not workingNIAttributedLabel settextcolor 不起作用
【发布时间】:2012-05-24 11:00:06
【问题描述】:

我使用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 中,但最后一行无法正常工作。所有的文字都是蓝色的。

【问题讨论】:

  • stringPH 和 strEditedText 的值是多少?
  • strEditedText 是:请致电 800.493.0016 联系 abc,选项 #3 以获得进一步帮助。而 stringPh 是 800.493.0016
  • 你能在最后一行之前NSLog你的每个字符串吗?唯一可能导致这种情况(所有文本为蓝色)是两个字符串相同(因此范围从开始到结束)。我已经使用带有您提供的值的静态字符串复制粘贴了您的代码,它工作正常。

标签: iphone ios nimbus-ios


【解决方案1】:

我设置了一个示例,您可以下载here,我发现它运行良好。
与:

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

模拟器正确地突出显示文本的右侧部分:

您确定您的字符串stringPhstrEditedText 的实际子字符串吗?

【讨论】:

    【解决方案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];
    

    【讨论】:

      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多