【发布时间】: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