Swift - 修改UITextView中链接的样式(链接颜色、下划线样式)
作者:hangge | 2017-05-08 08:10
我们知道多行文本框(UITextView)具有 URL 检测功能,将其开启后,它会高亮显示内容中的 url 链接文字,点击后便会使用 safari 打开这个链接。
(2)粗线
(3)双线

之前有网友问这个链接的样式能否修改,比如加个下划线什么的。这个通过设置 textView 的 linkTextAttributes 属性就可以实现。
1,修改链接颜色
下面将链接颜色修改成橙色。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.text = "欢迎访问 http://www.hangge.com"
textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.orange]
}
}
2,给链接增加下划线
(1)细线
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]
(2)粗线
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue]
(3)双线
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue]
3,修改下划线颜色
默认情况下下划线的颜色和链接文字颜色是一样的,我们也可将下划线修改成其他颜色。
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
NSUnderlineColorAttributeName: UIColor.green]
全部评论(0)