gpt4 book ai didi

Swift - 带链接的 HTML 风格化 NSAttributedString

转载 作者:行者123 更新时间:2023-11-30 12:45:01 25 4
gpt4 key购买 nike

我有一个 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))

它删除了超链接...我希望它们协同工作。我做错了什么?

最佳答案

对于任何访问过的人来说,@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))

关于Swift - 带链接的 HTML 风格化 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com