Reputation: 13887
My App has a simple built in help page. The page is loaded from a rich-text file in to an NSAttributedString
, and the string is displayed in a UITextView
.
Here's a fragment of code from prepareForSegue:
where I load the NSAttributedString
prior to using it for display…
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];
This all works well. However, it doesn't support iOS's "Dynamic Type" – the fonts are baked in to the RTF file and don't react to the users's settings.
I'd like the guide to use the system defined styles for body: UIFontTextStyleBody
and heading: UIFontTextStyleHeadline
.
How might I do this?
I'd be fairly happy for the guide to be defined in another format such as HTML or Markdown. I'd prefer not to add external libraries to the project if I can avoid them, though.
Upvotes: 3
Views: 637
Reputation: 15331
You have access to preferred fonts through CSS. For example you could create an html document like:
<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>
Then replace NSRTFTextDocumentType
with NSHTMLTextDocumentType
in the code you have above.
The full list of preferred font names in CSS is:
-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
If you don't want to define the fonts on every element, you can modify the styling in the html <head>
tag like this:
<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>
Upvotes: 3