我有一个UITextView,我有一个HTML字符串,我就像这样......
func applyHTML() {
textView.attributedText = htmlString()
textView.linkTextAttributes = [NSForegroundColorAttributeName: .blue,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
NSFontAttributeName:font]
}
func htmlString() -> NSAttributedString? {
if let htmlData = text.dataUsingEncoding(NSUnicodeStringEncoding) {
do {
let attributedString = try NSMutableAttributedString(data: htmlData,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attributedString
}
catch { return nil }
}
return nil
}
这很好用,我得到带有黑色文字的蓝色超链接......我现在要做的是为非超链接文本的所有颜色和颜色设置样式...问题是当我这样做时,通过添加这个....
let length = attributedString.length
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
attributedString.setAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))
它删除了超链接...我希望它们协同工作。我做错了什么?
答案 0 :(得分:0)
对于任何访问者,@ Larme都是对的
attributedString.setAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))
应该是
attributedString.addAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))