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:

  • Xcode 5/6: View Debugging always grayed out?
  • What exactly does delegate do in xcode ios project?
  • Entitlements file do not match those specified in your provisioning profile.(0xE8008016)
  • Automatic @property synthesize not working on NSManagedObject subclass
  • Objects Sorting With date ,Time Problem in Array(Iphone Development)
  • CMake link_directories from library
  •  -(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

    9 Solutions Collect From Internet About “Make link in UILabel.attributedText *not* blue and *not* underlined”

    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.

    I am using TTTAttributedLabel. I wanted to change the color of the linked text, and keep it underlined. Pim’s answer looked great, but didn’t work for me. Here’s what did work:

    label.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor magentaColor],
                               (id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle] };
    

    Note: if you don’t want the text underlined, then remove the kCTUnderlineStyleAttributeName key from the dictionary.

    Here’s is my improved version of Ramsel’s already great answer.
    I believe it’s much more readable, and I hope it will come to good use.

    label.linkAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor],
                              NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
    

    Here’s a list of other attibute names.

    If you are using a uitextview, you might have to change the tintColor to change the link color

    Swift 2.3 example for TTTAttributedLabel:

    yourLabel.linkAttributes       = [
        NSForegroundColorAttributeName: UIColor.grayColor(),
        NSUnderlineStyleAttributeName: NSNumber(bool: true)
    ]
    yourLabel.activeLinkAttributes = [
        NSForegroundColorAttributeName: UIColor.grayColor().colorWithAlphaComponent(0.8),
        NSUnderlineStyleAttributeName: NSNumber(bool: false)
    ]
    

    Swift 3 example for TTTAttributedLabel:

    yourLabel.linkAttributes = [
        NSForegroundColorAttributeName: UIColor.green,  
        NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleNone.rawValue)
    ]
    yourLabel.activeLinkAttributes = [
        NSForegroundColorAttributeName: UIColor.green,  
        NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleDouble.rawValue)
    ]
    

    For Swift 3 using TTTAttributedLabel

    let title: NSString = "Fork me on GitHub!"
    
    var attirutedDictionary = NSMutableDictionary(dictionary:attributedLabel.linkAttributes)
    
    attirutedDictionary[NSForegroundColorAttributeName] = UIColor.red
    attirutedDictionary[NSUnderlineStyleAttributeName] =  NSNumber(value: NSUnderlineStyle.styleNone.rawValue)
    
    attributedLabel.attributedText = NSAttributedString(string: title as String)
    attributedLabel.linkAttributes = attirutedDictionary as! [AnyHashable: Any]
    
    let range = subtitleTitle.range(of: "me")
    let url = URL(string: "http://github.com/mattt/")
    
    attributedLabel.addLink(to: url, with: range)
    

    For Swift 3 following way worked out for me using TTTAttributedLabel:

    1) Add a label on the storyboard and define its class to be TTTAttributedLabel
    Storyboard view

    2) In code define
    @IBOutlet var termsLabel: TTTAttributedLabel!

    3) Then in ViewDidLoad write these lines

    let textString = "By using this app you agree to the Privacy Policy & Terms & Conditions."
    guard let labelString = termsLabel.attributedText else {
                return
            }
            guard let privacyRange = labelString.string.range(of: "Privacy Policy") else {
                return
            }
            guard let termsConditionRange = labelString.string.range(of: "Terms & Conditions") else {
                return
            }
    
            let privacyNSRange: NSRange = labelString.string.nsRange(from: privacyRange)
            let termsNSRange: NSRange = labelString.string.nsRange(from: termsConditionRange)
    
            termsLabel.addLink(to: URL(string: "privacy"), with: privacyNSRange)
            termsLabel.addLink(to: URL(string: "terms"), with: termsNSRange)
    
            termsLabel.delegate = self
            let attributedText = NSMutableAttributedString(attributedString: termsLabel.attributedText!)
            attributedText.addAttributes([NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 12)!], range: termsNSRange)
            attributedText.addAttributes([NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 12)!], range: privacyNSRange)
            attributedText.addAttributes([kCTForegroundColorAttributeName as String: UIColor.orange], range: termsNSRange)
            attributedText.addAttributes([kCTForegroundColorAttributeName as String: UIColor.green], range: privacyNSRange)
            attributedText.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue], range: termsNSRange)
            attributedText.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue], range: privacyNSRange)
            termsLabel.attributedText = attributedText
    

    It would look like this
    Simulator View

    4) Finally write the delegate function of TTTAttributedLabel so that you can open links on tap

    public func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
        switch url.absoluteString  {
        case "privacy":
            SafariBrowser.open("http://google.com", presentingViewController: self)
        case "terms":
            SafariBrowser.open("http://google.com", presentingViewController: self)
        default:
            break
        }
    }
    

    Swift 3:

    // apply link attributes to label.attributedString, then
    label.tintColor = UIColor.red // any color you want