如何将 HTML 的 CSS 添加到 NSAttributedString? [英] How to add CSS for HTML to NSAttributedString?
本文介绍了如何将 HTML 的 CSS 添加到 NSAttributedString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 NSAttributedString 加载 HTML 文件,以将其显示在UITextView.
I am trying to load an HTML file via NSAttributedString to display it in a UITextView.
那么,如何将 CSS 应用于文本视图的内容?
So, how can I apply CSS to the text view's content?
推荐答案
从 iOS 7 开始,你可以使用 NSAttributedString
和 HTML 语法:
Since iOS 7 you can use NSAttributedString
with HTML syntax:
NSURL *htmlString = [[NSBundle mainBundle] URLForResource: @"string" withExtension:@"html"];
NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
textView.attributedText = stringWithHTMLAttributes; // attributedText field!
你必须将string.html添加到你的项目中,html的内容可以是这样的:
You have to add string.html to you project, and the content of the html can be like this:
<html>
<head>
<style type="text/css">
body {
font-size: 15px;
font-family: Avenir, Arial, sans-serif;
color: red;
}
</style>
</head>
<body>
<p>This is the text</p>
</body>
</html>
根据我的测试,您可以更改:颜色、字体系列、字体粗细、字体大小、文本对齐,并使用 <b>
、<strong>
、、
、
和
;
标签.
From what I could test, you can change: color, font-family, font-weight, font-size, text-align, and use <b>
, <strong>
, <i>
, <u>
, <sup>
, and <sub>
tags.
这篇关于如何将 HTML 的 CSS 添加到 NSAttributedString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文