【问题标题】:UITextContainerView hidding clickable link of UITextViewUITextContainerView 隐藏 UITextView 的可点击链接
【发布时间】:2017-10-04 13:07:20
【问题描述】:

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

正如我们所看到的:漂浮在我的 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 做的。

【问题讨论】:

  • stackoverflow.com/questions/46547066/… 相比有何变化?另外,您是否按照我在上一个问题中的建议实现了处理链接的UITextViewDelegate 方法?那个方法被调用了吗?
  • 是的,我做了一个随机的 textView 并尝试了链接并且它有效,它似乎不适用于这个特定的一个

标签: ios swift uitextview


【解决方案1】:

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

【讨论】:

  • 这个错误:NSAttributedStringKey' (aka 'NSString') has no member 'link'
  • @BigBurger 检查你的 swift 版本
猜你喜欢
  • 2016-11-04
  • 2015-09-16
  • 2022-12-10
  • 2018-02-18
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多