Question

My project consists of displaying a document in a UITextView. Most of the document is pretty straight forward attributed text and I have that working just fine. There are other parts of the document that consist of normal text and then a 3 column table with 2 or 3 rows basically right in the middle of the normal text.

After spending a fair amount of time searching the web I can't seem to find any reference to anyone doing anything like this.

Based on some tutorials I have read, I was thinking that maybe there was a way to create a subview consisting of a bunch of UILabels that would be subviews of a UIView (We'll call it _tabularView)that I could layout on top of the UITextView. Then I could somehow position the frame of _tabularView to the character range in the UITextView where it is supposed to be. I could then use an exclusion path to keep the text from going under the view. My only problem is that I am not exactly sure how to go about doing this and I am also worried that it wouldn't scroll with the rest of the content.

My question is, does anyone have any suggestions on how to accomplish this or a suggestion for a better way to embed a table in a UITextView.

Was it helpful?

Solution

One easy possibility is to use a UIWebView instead of a UITextView. The user isn't going to be able to edit the table anyway, so if the whole text view is not supposed to be editable, this is an easy choice.

If the text view is to be editable, then draw the table into an image context and include the image inline in the text view (iOS 7 only, because it gives you the full power of Text Kit). The table still won't be editable, because it is an image. But that shouldn't bother you, since your proposed labels wouldn't have been editable either.

I should also mention that if the text is all short (single words, essentially) and if you don't want to draw grid lines, you could just do it with tabs (another new iOS 7 feature).

OTHER TIPS

Here are some possibilities for you to work around your problem:

  • Maybe using a custom library for showing the grid would be overkill, but try out AQGridView.
  • You could make the custom UI yourself by using scrollviews and labels according to the grid. Similar SO post here.
  • As Matt says in his answer, using a UIWebView would probably be the cleanest solution as you could display your table in html.
  • Lastly, the inline image option for the table. First make the table of data in a UIView (offscreen) and take a snapshot into a UIImage which you can add inline. Here is a SO post for that.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top