2014-10-07 36 views
1

我有2个字的字符串。我想为不同颜色的2个单词打上红色和绿色。 另外它的整个字符串应该有可点击的链接。我试着用下面的代码,链接属性覆盖了以前的颜色。带链接的NSAttributedString

NSString *str = @"xxxx yyyyyy"; 

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str]; 
[attStr addAttribute:NSForegroundColorAttributeName 
        value:[UIColor redColor] range:NSMakeRange(0, 4)]; 
[attStr addAttribute:NSForegroundColorAttributeName 
        value:[UIColor blueColor] range:NSMakeRange(5, str.length-5)]; 

NSMutableAttributedString *clickStr = [[NSMutableAttributedString alloc] initWithAttributedString:attStr]; 

    [clickStr addAttribute:NSLinkAttributeName value:@"textLink:link" range:NSMakeRange(0, str.length)]; 
+0

你想要显示的链接是什么?当然不是文字颜色,对吧?下划线?您可以简单地将其添加为字符串整个范围的另一个属性。 – NRitH 2014-10-07 15:18:00

+0

我想保留在链接属性之前添加的文本颜色。我不关心下划线。 – turbo 2014-10-07 17:26:55

+0

这就是我要问的 - 虽然 - 如果你想要红色和蓝色的颜色,那么你想添加到字符串的链接是什么? – NRitH 2014-10-07 21:52:57

回答

0

据我所知,你不能覆盖默认属性字符串中的链接样式。你必须使用类似TTTAttributedLabel的东西,尽管它仍然在iOS 8上有一些bug。