Displaying Lots Of RichText : Choosing the best option - iphone

What is the best way to display lots of RichText (HTML formatted)
At the moment I'm working on an app for a forum. As you'd expect the posts on the site have lots of HTML formatting, lists, images bold text, colored text, etc...
Now there are several options I can think of (non of them ideal, please if you think of any other options post them in the comments):
Custom Cells using NSAttributedString+HTML + DTAttributedTextView for each post?
Problems: I used the NSAttributedString+HTML categories in the app elsewhere and it was less then ideal, creating an NSAttributedString seems to be quite expensive (slow) even in small amounts.
For 10+ posts each of which may be the length of an entire article would be awful + although DTAttributedTextView supports the IMG html tag (an most tags) it doesn't support remote loading of images unless you specify their width and height in the img tag. And for a forum where an IMG tag could be a smiley (10*10) or a screenshot (640*960) there's no way to predict that.
(Since writing this NSAttributedString+HTML, which is now renamed DTCoreText, has added full support for <img> tags and improved greatly!)
Custom Cells with a UIWebView in them for each post?
This one I considered for quite a while, however when reading this blog post I realised a problem that would cause, Smooth scrolling. The idea of having a native application for a site is that it is better then using a simple UIWebView to view the sites mobile theme. If the app lags and is jerky while scrolling that is worse not better (also as I need to display images hiding the webview's like he suggests wouldn't work). Also UIWebView's need to be created on the main thread or they break.
Back to the UIWebView?
Annoyingly besides doing a pathetic cheat, (like in the iFans app) where you only display text and then if they click it a UIWebView loads with all the nice images etc..., the only option left seems to be to do what I think the TapaTalk app does and have the entire thread view as a UIWebView. This isn't too bad as it will probably have quite good performance and will allow me to possibly add user controlled themes etc.. but I find the idea of using a UIWebView in a websites native app repulsive.
Does anyone have any experience creating web powered app, like maybe a facebook client, forum app, or new site app which had to display content from the site (I don't really count a twitter client as it only has to deal with text & links in small amounts per post). Or any ideas on the best way to display RichText content in an iOS app?
Any idea's would have to deal with the lot:
Multi Coloured Text.
Right, Left & Center aligned text.
Images (of variable size).
Bold text.
Text of different sizes & fonts.
Underlines text.
YouTube Videos.
HTML tables.
Just in case the actual question wasn't very clear in all of that I'll sum it up:
"What is the best way to display lots of RichText (HTML formatted) content for a forum client app"

So if I am reading this right, you want the forum posts of a given topic to be cells of a UITableView and the cells need rich formatting?
Assuming this is correct, I imagine each post will take up a lot of the screen (large cell height). In this case, having a UIWebView as a subview might not be as bad performance-wise as you might think. UITableView reuses cells so really only the visible cells need be loaded into memory.
I'm also assuming you are able to access the forum via an API, right? In which case, you should be able to pre-load data immediately prior to loading the view and the UIWebViews will only be used for formatting. You can even load a CSS file from your app bundle when you loadHTMLString into your UIWebView so you're not loading that from a server every time.
All that being said, if you did have a lot of concurrently visible cells it might be a different story and I'd maybe consider only showing plain text in the UITableView index and displaying the rich formatting only when the user taps the cell to view the single post. This might also be better from a design standpoint as having a ton of differently formatted cells on screen could potentially end up looking a bit sloppy.
Also, this may be obvious (especially since you seem to be both performance and design conscious) but please don't use UIWebView for UI controls. Any time I see a UIWebView tab bar or fake navigation bar I cringe (ack, Netflix). For formatting though, a lot times it's the only way to fly if you're loading a lot of dynamic content from a server.

NSAttributedString+HTML already supports lazy loading of images. See the Demo that shows how to do that. It's reasonably fast but you want to hold of parsing the HTML for content that is not on screen.
It mostly depends on what UI you want to achieve. NSAS+HTML is meant for situations where you control the quality of the HTML (i.e. because you generate it yourself). There it affords you with an unprecedented level of control over the view hierarchy because you can embed your own custom views to show images.
In other cases where you cannot be sure about the quality of the HTML you have to use UIWebView and work around all of it's limitations, one being that it takes enormous amounts of RAM and it slow to show the contents. Also UIWebView is not thread-safe (because WebKit is not).

Related

Is it a good idea to add uiwebviews in table cells?

I have several local html files and I want to show them in a table with static(for now) cells.
Each UIWebView would have a different height (I dont want to have any scrolls in the UIWebView) so obviously each cell has a different height. Most of the comments i read discourage the usage of UIWebView in table cells. What do you think : is this applicable ? will it be slow and need time to load?
UIWebview in tableview cells will affect the performance for sure. To display Html content in tableview cells you can use NSAttributedString.
I used a UIWebView in an app to display content coming from the web as rows in a table view.
I know that doing that is discouraged, due to the high memory use of UIWebView, but in my case it was not only a matter of displaying rich text: I had to display HTML loaded from the web, so I give that option a try. In short, I tried it and it worked fine.
The app got memory warnings from time to time, but it was also able to recover memory effectively and run unaffected even on an ipod touch (just 256MB of memory). Performance was possibly affected, but due to the fact that the app was doing just that (loading text from the web and displaying it in the table), the app was perfectly responsive.
One important notice: pages were pretty simple, just text, and each table did not contain more than 15 elements. Furthermore, one thing you have to consider is that the UIWebView introduces a small delay in the rendering, so you will have to deal with it and accept it in your app (there is no way to get rid of it that I know of).
In a later project, I used a different approach: using just one UIWebView and creating the table using HTML/js inside of it. Well, it also worked, but I could not see any big improvement as to peak memory usage nor performance (I checked with instruments).
This is just my experience, I am not trying to say that this will work or that is good practice to do. In any case, I hope it helps.

How would you design a question/answer view (iPhone SDK)

I'm new to iPhone development, and I have a question on how to create a view for my application.
The view should display a problem (using formatted/syntax highlighted text), and multiple possible answers. The user should be able to click on an answer to validate it.
Currently, I am trying to use a UITableView embedding UIWebView as contentView. That allows me to display formatted text easily.
The problem is that it is a real pain to compute and adjust the height of the cells. I have to preload the webview, call sizeToFit, get its height, and update the cell accordingly. This process should be done for the problem and the answers (as they are HTML formatted text too).
It's such a pain that I am planning to switch to something else. I thought using only a big UIWebView and design everything in HTML. But I looked at some articles describing how to communicate between the HTML page and the ObjectiveC code. This seems to involve some awful tricks too...
So... that's it, I don't really know what I should do.
I guess some of you dealt with such things before, and would provide some greatly appreciated tips :)
The catch here is that the iPhone API does not yet support NSAttributedString so you can't just set the text to appear as you would like in a textview.
I saw one work around which essentially used individual UILabels to represent each attribute run. (Can't find the link now.) They used NSString UIKit extensions to calculate the position of the strings on the view and then used that to position the labels.
Another work around would be to draw the strings with their attributes to a UIImage and then just display the image. That would be the easiest solution I think.
In either case your going to have to basically recreate the data structure of an attributed string.
NSAttributedString does a lot of work for us. We really miss it when it is gone.

Is there a HTML wrapper in Cocoa other then UIWebView?

I have a bunch of texts and images (taken from the content tag of a RSS feed item) that I want to display in my app. I've managed to extract them from the entire content tag with some regular expressions. But the thing is, in order for the texts to appear before all the images are loaded, I need to preload all the images, and even more, I need to reposition all the texts/images when an image is loaded, because I don't know their size at first, to position the element under them correctly.
I realized this is too much hard-work for such a simple task.
I searched for some simple HTML wrapper but I found nothing. And than I realized: hey, I can insert HTML directly into an UIWebView. But then again, I see UIWebView more like an iFrame in HTML, and by that I mean not a very flexible/fluid element. The content will be bigger than the iPhone screen height, can the UIWebView adjust to fit it's contents? I don't want the browser zoom features and all, but rather to blend in the page.
So bottom line: In order to display a bunch of texts combined with images, should I continue with my initial pain-in-the-ass method, should I use a UIWebView, or is there another simple element like the one in my dreams? :)
Thanks.
Definitely use the web view; it has hundreds of person-years of work behind it, and is realistically impossible for you to reproduce by yourself. To keep it from zooming, you can add a viewport meta tag to your HTML fragment.
...[S]hould I continue with my initial pain-in-the-ass method, should I use a UIWebView, or is there another simple element like the one in my dreams...
I'm not entirely sure what you have against UIWebView, it's a decent and fairly complex element that can support many behaviors. One of the extremely attractive properties of IB and Cocoa development is that prototyping is very quick. I think you should spend half an hour and play around with the component. Writing your own code is definitely an option, but layout engines (ie WebKit in UIWebView/Safari, or Gecko in Firefox) is a complicated task. Why reinvent the wheel?
HTH.

How to lay out an iPhone page

I want to implement a description page in an iPhone app. It's the final page in a drill-down navigation structure and needs to include a title, description, icon, large image, date and some buttons the user can click on to activate another application. The information comes from an external source and the title, description and image all vary in size.
I've seen various ways of doing this, such as laying out each piece of information inside table cells, and creating views that look like web pages. I've come up against layout problems using a standard UIView and UILabel because of the variable size content.
Looking at other apps, I think Facebook and the App Store have the most attractive layouts and handle variable size content well. I'd like to know what the easiest way of getting an attractive layout is, and also which way will produce the best (as in best looking) results.
a few things first you are in luck because a lot of the facebook UI stuff is available in the three20 project
http://github.com/joehewitt/three20/tree/master
there are a lot of templates that you can use in there that might solve all your problems, however documentation is a bit light on, and it may take some reverse engineering.
Also, you didn't mention the UIscrollview this might help you get a layout with more information on the page.
To get the best looking apps unfortunately you need to have the eye for detail (or pay someone who does) read through apples Human interface guidelines, as they seem to know what they are doing.

Better way to have a photo slider/thumbnails on iPhone?

I have a probaly large list of images and want something like the photo app but with custom toolbar.
I wonder how do this. I see the sample of Apple http://developer.apple.com/iphone/library/samplecode/Scrolling/index.html but that not will work for a large set of images.
I have a product list and some of my customer need download as much as 12,000 products in the device (I have a solution for the PocketPC I'm porting to iPhone).
In this thread How make a view of thumbnails in landscape mode? somebody suggest is possible use a TableView, but then I'm not sure... mainly how hide the section caption then could be...
UPDATED correct the link
The "Scrolling" demo won't work as-is for a large image set, but some minor adjustments can easily fix that. The trick is to use UIScrollView delegate methods to load images on demand and then unload them after they disappear. For decent performance you'd want the central image and one or two on either side to be loaded-- meaning that you can extend that design to support as many images as you like but still never have more than 3-5 in memory at a time.
The Stackoverflow link you post doesn't actually mention table views, so I'm not sure what approach you have in mind there. If by "section caption" you're referring to section header and/or footer text, then just don't provide any. Table views don't have a default for this, so if you don't give one to a table view then it won't show one.