ios-属性字符串,某些字符下划线

时间:2018-10-26 06:46:08

标签: ios swift nsattributedstring

我有一个文本视图,其中有一些包含电子邮件的文本。我想在电子邮件中添加下划线

我已经使用以下代码成功完成了

//Attributed string generation
let attributes:[NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor(red: 82.0/255.0,
                                                    green: 82.0/255.0,
                                                    blue: 82.0/255.0,
                                                    alpha: 1.0),
    NSAttributedString.Key.font: UIFont.customAppFont(with: 14.0)
]

let mail = "BECPgjygnang@customercare.com"
let mailDescription = "For communication please contact \(mail)"
let mailDescriptionAttributed = NSMutableAttributedString(string: mailDescription, attributes: attributes)

//Adding link to the email range
let mailDescriptionString = NSString(string: mailDescription)
let range:NSRange = mailDescriptionString.range(of: mail)
mailDescriptionAttributed.addAttribute(.link, value: "mailTo://?mail=\(mail)", range: range)

//Setting up link attributes
let linkAttributes:[NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor(red: 89.0/255.0,
                                                    green: 10.0/255.0,
                                                    blue: 115.0/255.0,
                                                    alpha: 1.0),
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
self.messageTextView.linkTextAttributes = linkAttributes
self.messageTextView.attributedText = mailDescriptionAttributed

但是问题是某些字符的下划线中断。

enter image description here

您可以清楚地看到字符“ g”,“ y”,“ j”和“ @”下划线断开。任何人都可以建议如何避免它。谢谢。

0 个答案:

没有答案