【问题标题】:How apply CSS to a iOS NSAttributedString如何将 CSS 应用于 iOS NSAttributedString
【发布时间】:2016-12-07 20:11:53
【问题描述】:

我在一个 iOS 项目中工作,我有这个 HTML 文本需要在 UITextView 中显示:

<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,因为目前,颜色、字体大小和对齐方式都不起作用。

有人知道怎么做吗?

提前致谢!

【问题讨论】:

    标签: html ios css objective-c


    【解决方案1】:

    你可以喜欢这种方式

    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 文本的不同部分设置了不同的字体、大小和颜色,我需要的是能够在文本中识别这样的标签:span class="wysiwyg -color-d24b57"
    • @jigneshVadadoriya 如何添加外部 css
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2014-01-14
    相关资源
    最近更新 更多