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)];
你想要顯示的鏈接是什麼?當然不是文字顏色,對吧?下劃線?您可以簡單地將其添加爲字符串整個範圍的另一個屬性。 – NRitH 2014-10-07 15:18:00
我想保留在鏈接屬性之前添加的文本顏色。我不關心下劃線。 – turbo 2014-10-07 17:26:55
這就是我要問的 - 雖然 - 如果你想要紅色和藍色的顏色,那麼你想添加到字符串的鏈接是什麼? – NRitH 2014-10-07 21:52:57