Question

I am working on an in-app guide for my app where I want to layout some headings and paragraphs of text that support Dynamic Type.

What is the simplest method to with a single UITextView? (The text is Preferably loaded from a separate text file).

There is a lot of approaches to this:

  • UIWebViews (over kill??)
  • UITextView and NSAttributedString?
  • Text Kit - which seems cumbersome for this simple task?

Example of desired layout/functionality

Was it helpful?

Solution

UIWebView is by far the simplest solution for this problem. You can create the html file as a separate file in your project and load it from the bundle at runtime.

NSString *path = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:file baseURL:baseURL];

Using the baseURL trick will allow you to even use inside the html and have it find the images in your app bundle.

OTHER TIPS

Using a UITextView should give you what you need. Read the text from your file and set the font according to the category size determined by the user. Since iOS7, you can load a simple HTML file as an attributed string using initWithFileURL:options:documentAttributes:error:, so you can set simple styles and text inside the file. Use font:-apple-system-headline;, font:-apple-system-subheadline; and font:-apple-system-body; as your font in the HTML to get the correct font in the attributed string when reading an HTML file.

You can listen for UIContentSizeCategoryDidChangeNotification notifications to hear when the user has changed his preferred category and set your font or reload your data accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top