0

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

Roggie
  • 1,157
  • 3
  • 16
  • 40
  • Are you only wanting the hyperlink part to be underlined, and the rest to look normal, sort of like the UILabel above it? – David Chopin Sep 26 '19 at 01:22
  • Also, are you wanting the hyperlink to be the same text color as the rest of the attributed text? – David Chopin Sep 26 '19 at 01:22
  • @DavidChopin yeah, I only want the hyperlink part to be underlined, and the rest to look normal, like the UILabel above it. That is how it was prior to installing iOS13. Also, yes, I also want the hyperlink to be the same text color with the exception that it is underlined. – Roggie Sep 26 '19 at 01:25
  • Im going to see if I can come up with an extension function that works on iOS 13 for you – David Chopin Sep 26 '19 at 01:27

1 Answers1

1

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.

David Chopin
  • 2,780
  • 2
  • 19
  • 40
  • not working, I get an all black background. Have you changed the `iOS Deployment Target = iOS 13.0` in Build Settings of your project? – Roggie Sep 26 '19 at 01:39
  • I've updated my question with a screenshot of the output of your suggested answer so you can see. – Roggie Sep 26 '19 at 01:42
  • I do have my deployment set to 13, but am not getting this behavior. Can you try commenting our the line in which you call `disclaimerTextView.hyperLink(originalText:, hyperLink:, urlString:)`? Just to make sure that the function is really what's causing this behavior – David Chopin Sep 26 '19 at 01:45
  • I did comment it out and was getting the same behavior, I then changed the 'background' color from 'white' to 'clear' from the Storyboard, uncommented the line, re-run and its now working. :) – Roggie Sep 26 '19 at 01:52
  • 1
    thanks for your help, it appears that the Background color setting in the Storyboard was causing the issue, as my original extension also works lol :) – Roggie Sep 26 '19 at 01:54
  • All good :) sometimes we just need a second brain to help us notice obvious things. I figured it had something to do with interface builder stuff. – David Chopin Sep 26 '19 at 02:00
  • @DavidChopin Can you help me to solve this issue in iOS13.I tried all possible way but no luck... https://stackoverflow.com/questions/58405106/ios13-uitextview-converting-uitextview-to-image-is-not-working-properly-working – Aslam Oct 21 '19 at 03:43
  • Unfortunately @IosDeveloper , I am no good with `UIGraphicsBeginImageContextWithOptions`. I understand what it does (creating an image from things displayed on the screen), but I've only used it with a `UITableView` before. And even then, it was mostly a process of copying and pasting. Have you considered putting a bounty on it? – David Chopin Oct 21 '19 at 03:46