Best way to load existing, formatted text into UITextView?

Hi,


I am new to programming.


I am currently working on a app that provides popovers with a UITextView when the user touches a certain part of an image. The popover's purpose is to provide a textual description of the area of the image where the user touched. The text is loaded from an array of dictionaries and it is different for each touch location.


The problem is that my text has formatting. Paragraphs, lists, headers in bold, etc. At the moment, my text is in a MS Word file, formatted, and it is around 15k words long.


My original plan was to copy the text into JSON, then parse the JSON into an array and access the text as needed. However, the formatting is lost in JSON. At least, as far as I know, there is no way or tool that can convert the text and keep the formatting. So I will have to edit every string (around 300 of them, some fairly long, with complicated formatting) manually, with escape characters, so that new lines and TABs will show properly in the UITextView. This will take me days to acomplish. What is worse, it will be really hard to maintain/update the JSON simply becaues the result is hard to read.


I am wondering if there is a better way to load this text. Is there a tool, that understands formatted text from a word processor, and then it creates a String that will display properly?


Thanks for any help!

Replies

TextView isn't your friend if formatting is your goal. Use a webview, instead. See the docs in the dev center for details.

Post not yet marked as solved Up vote reply of KMT Down vote reply of KMT
  • I'm not sure if it's too late to ask this; But I take a simple Text file, formatted with tabs, it looks fine in Text Edit on Mac, it even displays nice on the console when printed out. But the layout is all messed up when I write it into the Text View window. If I use Webview, will I be able to edit the text and then send it to my PDF creator code? I would love an answer as I really need to edit a file and create a pdf of the edited file. By the way, I'm using Storyboards and Objective C vs Swift and Swift UI...

Add a Comment

The easiest way to do this is to convert your Word document to RTF. You can do this by copying and pasting the contents into TextEdit, and saving the result as an RTF file (not an RTFD). Then, you can read in the file into an NSAttributedString (there's init(URL:options:documentAttributes:) for this), which you can set as the value for the attributedText property on a UITextView.

Thank you for the answer!