UITextContainerView hidding clickable link of UITextView

Viewed 360

I made a clickable link in a UITextView using NSMutableAttributeString. All it changes is that the text is highlighted

The link is on the telephone number

DEBUGGER As we can see : floating over my UITextView there is a UIContainerView (I really don't know if its because of that.. I'm trying)

Here is my UIView code: class InfoBox: UIView {

let Heading: UITextView = {
    let textView = UITextView(frame: CGRect(x: 15, y: 0, width: 200, height: 35))
    textView.font = UIFont.systemFont(ofSize: 20)
    textView.textColor = UIColor.white
    textView.isScrollEnabled = false
    textView.backgroundColor = UIColor.clear
    textView.isEditable = false
    textView.isSelectable = true
    return textView
}()

let TextContent: UITextView = {
    let textView = UITextView(frame: CGRect(x: 15, y: 27, width: UIScreen.main.bounds.width, height: 30))
    textView.font = UIFont.systemFont(ofSize: 17)
    textView.textColor = UIColor.white
    textView.isScrollEnabled = false
    textView.backgroundColor = UIColor.clear
    textView.isEditable = false
    textView.isSelectable = true
    return textView
}()}

The NSAttributedString code:

        func transformText(text: String, underlined: Bool, linkURL: String) -> NSAttributedString {
    let textRange = NSMakeRange(0, text.characters.count)
    let attributedText = NSMutableAttributedString(string: text)
    if underlined{
        attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
        attributedText.addAttribute(NSUnderlineColorAttributeName , value: UIColor.lightGray, range: textRange)
    }
    attributedText.addAttribute(NSFontAttributeName , value: UIFont(name: "Helvetica-Light", size: 17)!, range: textRange)
    attributedText.addAttribute(NSForegroundColorAttributeName , value: UIColor.lightGray, range: textRange)
    if(linkURL != "")
    {
        let attrib = [NSLinkAttributeName: NSURL(string: linkURL)!]

        attributedText.addAttributes(attrib, range: textRange)
    }
    return attributedText
}

And this is how it is called:

    self.TelBox.TextContent.attributedText = transformText(text: self.TelBox.TextContent.text, underlined: true, linkURL: "https://www.google.fr")

Secondary question : is it possible to make a clickable link in a UITextView for a telephone number so that when clicked it calls that number? Did it with a UIButton .

1 Answers

I am not sure what is wrong with your UIContainerView, as far as i can see there's nothing wrong there.

Here's the method to make the link call a number:

func transformText(text: String, underlined: Bool, phoneNumber: String) -> NSAttributedString {
        let textRange = NSMakeRange(0, text.characters.count)
        let attributedText = NSMutableAttributedString(string: text)
        if underlined{
            attributedText.addAttribute(NSAttributedStringKey.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
            attributedText.addAttribute(NSAttributedStringKey.underlineColor , value: UIColor.lightGray, range: textRange)
        }
        attributedText.addAttribute(NSAttributedStringKey.font , value: UIFont(name: "Helvetica-Light", size: 17)!, range: textRange)
        attributedText.addAttribute(NSAttributedStringKey.foregroundColor , value: UIColor.lightGray, range: textRange)
        if(phoneNumber != "")
        {
            let attrib = [
                NSAttributedStringKey.link: URL(string: "tel://" + phoneNumber.replacingOccurrences(of: " ", with: ""))]
            attributedText.addAttributes(attrib, range: textRange)
        }
        return attributedText
    }

You can use it like this: TextContent.attributedText = transformText(text: "12345", underlined: true, phoneNumber: "12345")

Related