UITextContainerView隐藏UITextView的可点击链接

时间:2017-10-04 13:07:20

标签: ios swift uitextview

我使用NSMutableAttributeString在UITextView中创建了一个可点击链接。 它改变的全部是文本突出显示

The link is on the telephone number

DEBUGGER 我们可以看到:漂浮在我的UITextView上有一个UIContainerView(我真的不知道是不是因为它......我正在尝试)

这是我的UIView代码:     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
}()}

NSAttributedString代码:

        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
}

这就是它的名称:

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

次要问题:是否可以在UITextView中为电话号码创建可点击链接,以便在点击时调用该号码?是用UIButton做的。

1 个答案:

答案 0 :(得分:0)

我不确定你的UIContainerView有什么问题,据我所知,那里没有任何错误。

以下是使链接调用为数字的方法:

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
    }

您可以像这样使用它:TextContent.attributedText = transformText(text: "12345", underlined: true, phoneNumber: "12345")