gpt4 book ai didi

ios7 - 如何将 HTML 的 CSS 添加到 NSAttributedString?

转载 作者:行者123 更新时间:2023-12-01 22:07:50 26 4
gpt4 key购买 nike

我正在尝试通过 NSAttributedString 加载 HTML 文件将其显示在 UITextView 中.

那么,如何将 CSS 应用于 TextView 的内容?

最佳答案

从 iOS 7 开始,您可以使用 NSAttributedString HTML 语法:

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的内容可以是这样的:

<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> , <i> , <u> , <sup> ,和<sub>标签。

关于ios7 - 如何将 HTML 的 CSS 添加到 NSAttributedString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090597/

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