I want some words within my OHAttributedLabel to be links, but I want them to be colors other than blue and I don't want the underline.
This is giving me a blue link with underlined text:
-(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; }
Since I'm using OHAttributedLabel, I also tried using the methods in it's NSAttributedString+Attributes.h
category, but those return blue underlined links as well:
-(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; }
If I comment out the line setting the links in each version, the text gets colored to what I pass in - that works. It just seems like setting the link is overriding this and turning it back to blue.
Unfortunately the apple docs page I found shows how to set the link text to blue and underline it, exactly what I don't need: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html
You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks. However, that still means that what the user sees is the "raw" link. RTF files and HTML both allow you to set up a user-readable string with a link "behind" it.
So I ended up using TTTAttributedLabel:
-(void)createLinkFromWord:(NSString*)word withColor:(UIColor*)color atRange:(NSRange)range{ NSMutableAttributedString* newTextWithLinks = [self.label.attributedText mutableCopy]; NSURL *url = [NSURL URLWithString:@"http://www.reddit.com"]; self.label.linkAttributes = @{NSForegroundColorAttributeName: color, NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}; [self.label addLinkToURL:url withRange:range]; }
I found that OHAttributedLabel
actually does have methods to set links and declare colors and underline styles for those links. However, I wanted the links to be different colors based on a parameter. TTTAttributedLabel
allows this by letting you set it's linkAttributes
property for each link you create.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With