来自 RTF 的 NSAttributedString 使用 iOS Dynamic-Type
NSAttributedString from RTF using iOS Dynamic-Type
我的应用程序有一个简单的内置帮助页面。页面从 rich-text 文件加载到 NSAttributedString
,字符串显示在 UITextView
中。
这是来自 prepareForSegue:
的一段代码,我在使用 NSAttributedString
进行显示之前加载了它…
NSURL *const textURL =
[[NSBundle mainBundle] URLForResource: @"DontPanic"
withExtension: @"rtf"];
NSDictionary *const options =
@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType};
NSAttributedString *const text =
[[NSAttributedString alloc] initWithURL: textURL
options: options
documentAttributes: nil
error: nil];
一切正常。但是,它不支持 iOS 的 "Dynamic Type" – 字体被嵌入到 RTF 文件中,不会对用户的设置做出反应。
我希望指南使用 body 的系统定义样式:UIFontTextStyleBody
和标题:UIFontTextStyleHeadline
.
我该怎么做?
如果指南以 HTML 或 Markdown 等其他格式定义,我会非常高兴。不过,如果可以避免,我宁愿不将外部库添加到项目中。
您可以通过 CSS 访问首选字体。例如,您可以创建一个 html 文档,例如:
<html>
<body>
<h1 style="font: -apple-system-headine; font-family:-apple-system;">My heading</h1>
<p style ="font: -apple-system-body; font-family:-apple-system;">The body of this document</p>
</body>
</html>
然后将上面代码中的 NSRTFTextDocumentType
替换为 NSHTMLTextDocumentType
。
CSS 中首选字体名称的完整列表是:
-apple-system-headline
-apple-system-subheadline
-apple-system-short-headline
-apple-system-short-subheadline
-apple-system-body
-apple-system-short-body
-apple-system-tall-body
-apple-system-caption1
-apple-system-caption2
-apple-system-short-caption1
-apple-system-footnote
-apple-system-short-footnote
如果您不想在每个元素上都定义字体,您可以修改 html <head>
标签中的样式,如下所示:
<html>
<head>
<style type="text/css">
H1 {font: -apple-system-headine; font-family: -apple-system;}
P {font: -apple-system-body; font-family: -apple-system;}
</style>
</head>
<body>
<h1>This is a heading. It will be shown with the dynamic type headline font.</h1>
<p>This is a paragraph. It will use the dynamic type body font.</p>
</body>
</html>
我的应用程序有一个简单的内置帮助页面。页面从 rich-text 文件加载到 NSAttributedString
,字符串显示在 UITextView
中。
这是来自 prepareForSegue:
的一段代码,我在使用 NSAttributedString
进行显示之前加载了它…
NSURL *const textURL =
[[NSBundle mainBundle] URLForResource: @"DontPanic"
withExtension: @"rtf"];
NSDictionary *const options =
@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType};
NSAttributedString *const text =
[[NSAttributedString alloc] initWithURL: textURL
options: options
documentAttributes: nil
error: nil];
一切正常。但是,它不支持 iOS 的 "Dynamic Type" – 字体被嵌入到 RTF 文件中,不会对用户的设置做出反应。
我希望指南使用 body 的系统定义样式:UIFontTextStyleBody
和标题:UIFontTextStyleHeadline
.
我该怎么做?
如果指南以 HTML 或 Markdown 等其他格式定义,我会非常高兴。不过,如果可以避免,我宁愿不将外部库添加到项目中。
您可以通过 CSS 访问首选字体。例如,您可以创建一个 html 文档,例如:
<html>
<body>
<h1 style="font: -apple-system-headine; font-family:-apple-system;">My heading</h1>
<p style ="font: -apple-system-body; font-family:-apple-system;">The body of this document</p>
</body>
</html>
然后将上面代码中的 NSRTFTextDocumentType
替换为 NSHTMLTextDocumentType
。
CSS 中首选字体名称的完整列表是:
-apple-system-headline
-apple-system-subheadline
-apple-system-short-headline
-apple-system-short-subheadline
-apple-system-body
-apple-system-short-body
-apple-system-tall-body
-apple-system-caption1
-apple-system-caption2
-apple-system-short-caption1
-apple-system-footnote
-apple-system-short-footnote
如果您不想在每个元素上都定义字体,您可以修改 html <head>
标签中的样式,如下所示:
<html>
<head>
<style type="text/css">
H1 {font: -apple-system-headine; font-family: -apple-system;}
P {font: -apple-system-body; font-family: -apple-system;}
</style>
</head>
<body>
<h1>This is a heading. It will be shown with the dynamic type headline font.</h1>
<p>This is a paragraph. It will use the dynamic type body font.</p>
</body>
</html>