属性字符串下划线不适用于外部字体

时间:2018-11-14 08:37:25

标签: ios swift uitextview nsattributedstring

我有UITextView,并想在其中添加部分文本链接。这些嵌入的链接应带有下划线。当我将其font设置为内置文件之一时,它可以正常工作,但可以Ubuntu font即使设置了超链接颜色也不起作用。

let linkTextView = UITextView()
let linkAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: Design.colorPrimary,
        NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,]
linkTextView.linkTextAttributes = linkAttributes
linkTextView.isUserInteractionEnabled = false

let allText = "example link"
let linkText = "link"
let NStext = NSString(string: allText)
let attributedString = NSMutableAttributedString(string: allText)

attributedString.addAttribute(.link, value: NSURL(string: "https://www.apple.com")!, range: NStext.range(of: linkText))
linkTextView.attributedText = attributedString
linkTextView.heightAnchor.constraint(equalToConstant: 100).isActive = true

linkTextView.font = UIFont(name: "Ubuntu", size: 20)!//this does not
//linkTextView.font = UIFont(name: "ArialMT", size: 20)! // this works

ArialMT:

enter image description here

Ubuntu:

enter image description here

那我该如何解决呢?

1 个答案:

答案 0 :(得分:0)

在设置属性字符串之前,您是否尝试过更改字体?

当textColor不受字体影响时,更改字体时可以删除下划线。

致谢