Search code examples
swiftuitextviewios13

Customise embedded hyperlink in UITextView using Swift iOS13


Since installing iOS13 the UITextView extension is only customizing the background (to white color) around the text itself and not the entire UITextView.

How can I make sure that the entire UITextview object's background color is changed to white?

enter image description here

Extention:

extension UITextView {


    func hyperLink(originalText: String, hyperLink: String, urlString: String) {

        let style = NSMutableParagraphStyle()
        style.alignment = .center

        let attributedOriginalText = NSMutableAttributedString(string: originalText)
        let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
        let fullRange = NSMakeRange(0, attributedOriginalText.length)
        attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.white, range: fullRange)
        attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: fullRange)

        self.linkTextAttributes = [
            kCTForegroundColorAttributeName: UIColor.black,
            kCTUnderlineStyleAttributeName: NSUnderlineStyle.single.rawValue,
            ] as [NSAttributedString.Key : Any]

        self.attributedText = attributedOriginalText

    }//end func
}

Usage:

class LoginVC: UIViewController {

    @IBOutlet weak var disclaimerTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        disclaimerTextView.hyperLink(originalText: "By continuing, you agree to our Terms and Privacy Policy found on our website", hyperLink: "website", urlString: WEBSITE_URL)

    }
}

EDIT: output of the suggested solution - not working.

enter image description here


Solution

  • Changing the extension to this:

    extension UITextView {
        func hyperLink(originalText: String, hyperLink: String, urlString: String) {
    
            let style = NSMutableParagraphStyle()
            style.alignment = .center
    
            let attributedOriginalText = NSMutableAttributedString(string: originalText)
            let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
            let fullRange = NSMakeRange(0, attributedOriginalText.length)
            attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
            attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
            attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: fullRange)
    
            self.linkTextAttributes = [
                kCTForegroundColorAttributeName: UIColor.black,
                kCTUnderlineStyleAttributeName: NSUnderlineStyle.single.rawValue,
                ] as [NSAttributedString.Key : Any]
    
            self.attributedText = attributedOriginalText
    
        }
    }
    

    Is working for me. I simply removed the attributed text attributes that changed the foreground and background colors. I'm not sure why they were there to begin with.