iPhone - A text in a text - iphone

I would like to have a text in a text... I mean like in the newspaper: when there is a picture, the text fits and follow the border of the picture.
For example: http://en.wikipedia.org/wiki/Iphone
look at the pictures... the text follows them!
Is it possible to do that on the iPhone?
Thanks in advance

You can achieve this fairly easily with a UIWebView and traditional web techniques.
If you want to have it happen in Cocoa directly, you're going to have to dive into the more advanced CoreText APIs, which will probably be a lot of work.

It's called text wrapping or, in CSS/HTML speak, "floating" an image. The best way to do it is probably to have a UIWebView and programmatically dump HTML and CSS into it.
The HTML looks like this:
<img src="IMAGE_URL" style="float:right; margin:6px;" />
<p>My block of arbitrary test. Blah blah blah. Etc.</p>
Here's an example of how to use UIWebView with a string of HTML:
http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview

Maybe I'm missing something, but why not use subviews? Have a UIView containing a UIImage and UILabel in case of a static text or a non-editable UITextView in case of dynamic amount of text and set their properties to be aligned to the parent view's boundaries.
If you still want to take the low-level path, google for CGContextShowTextAtPoint.
You may also check out this: http://code.google.com/p/core-plot/source/browse/framework/Source/CPTextLayer.h

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

How to keep 2 different fonts within the same UITextField or UITextView?

I want to set 2 different fonts within the same UITextField and UITextView . How to do it?
Its a bit of work - you'll need to use Core Text and NSAttributedString to do this.
There are plenty of tutorials and examples, although I'd suggest using someone else's already-made UILabel subclass such as:
OHAttributedLabel
or
TTAttributedLabel
As these usually have some convenience methods to make handling a lot easier.
I would do it with 2 custom textfields overlaying, both backgroundcolor:clearColor, maybe stuffed on an image that represents the background.
I don't think it is possible to handle 2 different fonts within the same UITextField or UITextView. If you want to have different font style you can either set different font style within a UIWebView or use the coreText API.
Here are some links that might help:
iPhone Development - Setting UIWebView font
the official doc on core text: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
I know you already picked a valid answer but... don't do it that way... it's not worth it. Use a webview instead and draw everything with html.
In interface builder change Text View's Text field to Attributed. In that small editor that appears you can change the font/format/color of the selected text, like in any advanced text editor.

iPhone: How to higthlight word of UILabel

I would like to highlight a word which in an UILabel which is placed on an UITabelViewCell.
What I would like to do is set a background color for just that word.
Unfortunately you cannot use a UILabel for this. However, you do have options:
Use a UIWebView instead and set CSS for doing your highlight
<html>
<head>
...
</head>
<body>How is it <span class="highlight">going</span></body>
</html>
Use a custom label library: OHAttributedLabel or TTTAttributedLabel
Add a custom UIView behind your label and postion it over the text
Look into Core Text and create your own custom label
I'd strongly recommend looking at the second option.
Well UILabel does not support this, you will need to use CoreText.
You can use TTTAttributedLabel which wil make easier to use CoreText.
And grab NSAttributedString+Attributes for east manipulation of NSAttributedString.
I don't think this is possible. You need to make several UILabels for separating highlighted text from unhighlighted and put them one by one and, just setting background color of highlighted ones.
... or, don't use UILabel at all, if your text is dynamyc and it will be hard to separate words.

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.