text layout in custom rendered UITableViewCell - iphone

I am trying to custom render text within a UITableViewCell. This text can be over a number of lines and can contain http links, font and colour changes. I have looked at the sizeWithFont methods of NSString, but can't really work out a good way of rendering each part of the string so that it all runs together correctly.
Do I need to render each word separately to get it to fit properly, or is there a better way?

can you just generate appropriate HTML and render the whole thing with a UIWebView?

Related

Best way to display text with HTML tags in iOS application?

Currently I am working on iOS client for web-chat. Thus chat messages have HTML tags (bold, italic, underlined, font color, images (smiles), etc.). For example:
<b>bold</b> <i>italic</i> <!--smile:bird--><img style="vertical-align: middle;border: none;" alt="bird" src="http://www.site.com/engine/data/emo_chat/bird.gif" /><!--/smile--> ordinaty text
For the moment I have 2 ideas how to display messages:
Add UIWebView to tables cell. But I think that it's not an option, because we will have a lot of messages and a lot of WebViews.
Add UITextView to tables cell, parse HTML tags and make necessary changes to attributed string. But UITextView doesn't support images (if I am right).
Is there any other (better) way to display such data (styled text + images)?
Using a webview per-cell is not going to work as you suspect. Webviews take a noticeable time to render which means you will likely end up with old webview content being momentarily displayed in reused cells until the new content renders. Webview is also a pretty heavy-weight UI element and you will encounter performance issues with having many of them on the screen updating at once.
You will want to parse the HTML text you are given into an attributed string using a library like DTCoreText. From here, if you can target iOS 6 or later you can set the attributedText property on a standard UILabel. If you need to target earlier iOS versions you can again use DTCoreText, specifically the DTAttributedLabel or DTAttributedTextCell components.
The parsing and NSAttributedString rendering can all be done manually using an XML parser and CoreText, but DTCoreText will make your life much easier.
Update: You mentioned in a comment that you want support for <img/>. DTCoreText does have some basic image support, but this is a really hard problem if you are rendering text with CoreText because you have to make text flow around the image correctly, and reserve some space in the core text renderer to put your image into. If there is just a single image for each cell, I would suggest you manually extract the image path/url and lay it out with a UIImageView alongside your text.
You can get idea from RTLabel stuff. It is doing same thing which you want.
You can also convert HTML to NSAttributedString with native iOS classes. Look the following post
https://stackoverflow.com/a/18886718/1760527

In iOS, how do you embed images inline with text (in a UITextView)?

How can you create a UITextView that can have images inline? I looked up NSAttributedString, but it seems iOS does not support image attachments. Any ideas for an editable text view that can display images (I suppose RTF would do this)?
Thanks in advance for your help!
The only method I am aware of is to use a UIWebView. There is no way to add images to a UITextView, unfortunately, just plain text.
You could use the NSTextAttachment class. It lets you insert images inline of text.
I found an example of this in a cool project here: https://github.com/dzog/ImgGlyph. ImgGlyph provides UITextView and UILabel subclass implementations that help you replace specific strings with images and ensures they're sized correctly with respect to the text surrounding it.

Should I use UIWebView or UITextView to display text in an e-book reader app?

I want to make an e-book reader iPhone app. Should I use UITextView or UIWebView to display the text? Which control is used by other e-book readers?
I would use a UIWebView, as it gives you much more flexibility in the presentation of the text. According to the UITextView Class Reference:
This class does not support multiple
styles for text. The font, color, and
text alignment attributes you specify
always apply to the entire contents of
the text view. To display more complex
styling in your application, you need
to use a UIWebView object and render
your content using HTML.
Also, UITextView uses scrolling to display large amounts of text (it inherits from UIScrollView); in an e-book reader, you will most likely want to paginate the content, so you will not want the scrolling behaviour.
UIWebView is much better solution than UITextView mainly due to support of rich formatting of its contents. On the other hand you will miss some very important functions which you get for free while using UITextView. I'm talking mostly about searching inside, changing contents size etc. All of this is possible with UIWebView but it's not straightforward - css & javascript are for the help here
Did you get highlight functionality for this as font size can change. Save them for future so that when ever he came to same page can see them
U should use UIWEB view ,as to provide paragraph and other functions of text are not supported by text view, u can directly implement html code and can make the app with proper view of text. So my suggestion is to use web view.

Cocoa Touch text view with aligned, justified text

I am trying to create a textview with predefined height that will contain justified text.
When entering the text into the view, I need to be able to check when the view is full.
How would I do this? Core Text?
I am using sdk-3.2.
You cannot do it using UITextView (that would be the most "logical" way to do it), or any others SDK current classes.
The only simple way to do it if to put the content to be justified in an html file, justify it using CSS, and show it in a UIWebView.

Can't display selected text with different colors in UITextView, what should I do?

I want to display texts in UITextView with different colors and underline some of them (to show they are erroneous - commonly done in spellchecker) but that's not possible with the current version of UIKit. What other options should I pursue?
I am thinking of subclassing UIScrollView (as I am dealing with a lot of texts) and blit the text into a UIView (as the content) using CoreGraphics, and as new text is entered, I will blit new text onto it. Is that a good approach?
The recommended approach is to generate HTML and use a UIWebView. That is a scroll view so you should have all the functionality you need.
If you want more control over the text then your approach is good although you can probably use the UIKit NSString extensions to draw your text. Specifically, you want drawAtPoint:withFont: