views:

380

answers:

2

Hi,

I'm trying to use a UIScrollView as a UITextView, except it will have a logo at the top (thus I can not use UITextView directly).

In other words; I will have a UIScrollView with a UIImage and a UITextView in it. The content for the textView is localized and liable to change, so I can not hardcode the height of it. Is there any way to get the "this is how big this field should be to fit all the text"-value so I can properly calculate the neede height of the UIScrollView?

Thanks

+1  A: 

You can use the methods defined in NSString (UIStringDrawing):

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

This will return the size (width and height) of the text. You may want to add a few pixel for a margin.

Felix
This looks just right. Except it only ever returns "21" as a height :/ I assume this is the height of one row in my case. I'm using lineBreakMode UILineBreakModeWordWrap. The text has some formating in it (newlines). Could that be the problem?Thanks!
Jonatan Hedborg
+1  A: 

I would check out the UIStringDrawing Category for NSStrings which has the following method:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode

Make the size property a struct with the width you want and use a large height value:

CGSizeMake(width, 999.0f)
Matthew McGoogan
beaten to the punch!
Matthew McGoogan
This works! Thanks.
Jonatan Hedborg