OGeek|极客世界-中国程序员成长平台

标题: ios - UITextContainerView 隐藏 UITextView 的可点击链接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:54
标题: ios - UITextContainerView 隐藏 UITextView 的可点击链接

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

The link is on the telephone number

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

这是我的 UIView 代码: 类信息框: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 做的。



Best Answer-推荐答案


我不确定您的 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")

关于ios - UITextContainerView 隐藏 UITextView 的可点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46565704/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4