我希望OHAttributedLabel中的一些单词是链接,但我希望它们是蓝色以外的 colored颜色 ,我不希望下划线.
这给了我一个带下划线的蓝色链接:
-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{
NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];
[mutableAttributedText beginEditing];
[mutableAttributedText addAttribute:kOHLinkAttributeName
value:[NSURL URLWithString:@"http://www.somewhere.net"]
range:range];
[mutableAttributedText addAttribute:(id)kCTForegroundColorAttributeName
value:color
range:range];
[mutableAttributedText addAttribute:(id)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleNone]
range:range];
[mutableAttributedText endEditing];
self.label.attributedText = mutableAttributedText;
}
因为我使用的是OHAttributedLabel,所以我也try 了使用它的NSAttributedString+Attributes.h
个类别中的方法,但这些方法也会返回蓝色下划线的链接:
-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{
NSMutableAttributedString* mutableAttributedText = [self.label.attributedText mutableCopy];
[mutableAttributedText setLink:[NSURL URLWithString:@"http://www.somewhere.net"] range:range];
[mutableAttributedText setTextColor:color range:range];
[mutableAttributedText setTextUnderlineStyle:kCTUnderlineStyleNone range:range];
self.label.attributedText = mutableAttributedText;
}
如果我在每个版本中注释掉设置链接的行,文本就会根据我传入的内容上色——这很有效.看起来设置链接覆盖了这一点,并将其恢复为蓝色.
不幸的是,我找到的apple docs页面显示了如何将链接文本设置为蓝色并在其下方加下划线,这正是我不需要的: