gpt4 book ai didi

ios - UITextContainerView 隐藏了 UITextView 的可点击链接

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:30 24 4
gpt4 key购买 nike

我使用 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 做的。

最佳答案

我不确定你的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com