Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make link in UILabel.attributedText *not* blue and *not* underlined

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

like image 654
OdieO Avatar asked Mar 13 '13 09:03

OdieO


People also ask

How can I make a clickable link in an NSAttributedString?

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.


1 Answers

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.

like image 97
OdieO Avatar answered Sep 25 '22 21:09

OdieO