views:

1085

answers:

2

I have a line like:

 -Image-  This is simply a test 
 -Image-  but I want to know how
to separate the two lines.  What
to do here?

The best way I could figure to implement on the iPhone is:

UIImageView  UILabel1
             UILabel1
UILabel2

If there's an easier way to do this with the UI elements - i'm all ears. Otherwise, I'm trying to figure out where the text ends in UILabel1 without trial and error using sizeWithFont.

A more intricate way would be to find the size of each character at that font size using sizeWithFont, and add it all together (figuring out where the word wrap on line 1 of UILabel1, and then on Line2 as well). This feels like a hard way to solve an easy problem. Anyone know of the easy way?

+1  A: 

Perhaps this is not the answer you are looking for, but this sort of layout is easier done with a WebView.

From an efficiency point of view, UILabel text rendering is implemented via WebViews anyway.

This article by Craig Hockenberry suggests that trying to

use metrics and line breaking that [match] those used in UILabel’s implementation [...] is far from perfect

and also confirms that UILabels use WebViews under the hood (it's also easily seen in a Shark dump of a text-intensive app).

duncanwilcox
I agree it's easier in a UIWebView. I don't think that UILabels use WebView (do you know how expensive a WebView is?). They use drawTextInRect to draw the string. (I wish this was the answer -it's super easy). I didn't see reference in Craig's article (if u have other evidence - would love to c
The quote in Craig's article is "UILabel’s underlying WebView", check it out. As I said it's pretty easy to spot webcore code if you profile an app that uses a lot of text. WebKit is heavyweight, I was surprised to discover they use it for UILabels too.
duncanwilcox
A: 

NSString s = @"First Line \n second line \n lastline ";

oxigen