gpt4 book ai didi

html - 如何将 CSS 应用于 iOS NSAttributedString

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:01 25 4
gpt4 key购买 nike

我在一个 iOS 元素中工作,我有需要在 UITextView 中显示的 HTML 文本:

<a href="http://google.com" target="_blank" rel="nofollow" title="Link: http://google.com">http://google.com/</a><br><span class="wysiwyg-color-d24b57 ">TEST<br><i><span class="wysiwyg-color-704938 ">TEST2<br></span></i></span><div class="wysiwyg-text-align-left "><span class="wysiwyg-color-d24b57 "><span class="wysiwyg-color-704938 "><u><span class="wysiwyg-color-3a529c ">Test3<br>TEST4<br><br></span></u><span class="wysiwyg-color-3a529c wysiwyg-font-size-x-large ">Test5</span><u><span class="wysiwyg-color-3a529c "><br><br><br></span></u></span></span></div>

到目前为止,我已经创建了 NSAttributedString 和 UITextView:

NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [example.text dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];


UITextView *textView = [[UITextView alloc] init];
textView.frame = [node labelFrame];
textView.attributedText = attributedString;
textView.editable = NO;
textView.selectable = YES;
textView.backgroundColor = [UIColor clearColor];

return textView;

现在我必须在上面应用所见即所得的 CSS,因为目前颜色、字体大小和对齐方式都不起作用。

有人知道怎么做吗?

提前致谢!

最佳答案

你可以喜欢这样

let divHtml = "<div style=\"font-family:Helvetica;font-size:14px;line-height:22px\" >" + YOUR TEXT + "</div>"
let attributedString = try! NSAttributedString(data: divHtml.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

textView.attributedText = attributedString

关于html - 如何将 CSS 应用于 iOS NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016933/

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