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

前端 未结 11 1238
终归单人心
终归单人心 2020-12-24 05:18

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 lin

11条回答
  •  再見小時候
    2020-12-24 05:46

    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

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

    3) Then in ViewDidLoad write these lines

    termsLabel.attributedText = NSAttributedString(string: "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

    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
        }
    }
    

    Update for Swift 4.2

    For Swift 4.2, there are some changes in step 3, all other steps would remain same as above:

    3) In ViewDidLoad write these lines

    termsLabel.attributedText = NSAttributedString(string: "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([NSAttributedString.Key.font : UIFont(name: "Roboto-Regular", size: 12)!], range: termsNSRange)
            attributedText.addAttributes([NSAttributedString.Key.font : UIFont(name: "Roboto-Regular", size: 12)!], range: privacyNSRange)
            attributedText.addAttributes([kCTForegroundColorAttributeName as NSAttributedString.Key : UIColor.orange], range: termsNSRange)
            attributedText.addAttributes([kCTForegroundColorAttributeName as NSAttributedString.Key : UIColor.green], range: privacyNSRange)
        attributedText.addAttributes([NSAttributedString.Key.underlineStyle: 0], range: termsNSRange)
            attributedText.addAttributes([NSAttributedString.Key.underlineStyle: 0], range: privacyNSRange)
    

提交回复
热议问题