Я хочу, чтобы некоторые слова в моей OHAttributedLabel были ссылками, но я хочу, чтобы они были цветами, отличными от синих, и я не хочу подчеркивания.
Это дает мне синюю ссылку с подчеркнутым текстом:
-(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, я также попытался использовать методы в нем 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, которую я нашел, показывает, как установить текст ссылки в синий цвет и подчеркнуть ее, что именно мне не нужно: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html