1

I currently have an app that prints out a big text inside a TextView but I'm unsure how I can calculate what the height on the textView needs to be so it fits perfectly. Any help is greatly appreciated.

This is how my text would look like

This is a cover of Cimorelli's song Million Bucks with my own spin on it :-) Hope you like!!! Download free: http://davedays.com.hostbaby.com/files/Million_Bucks.mp3 (right click and save as)

Ending song "Boy You'll Forget" https://itunes.apple.com/us/album/boy-youll-forget/id605629968

Cimorelli: http://www.youtube.com/cimorellitheband Thanks to Jon for filming! http://www.youtube.com/simplyspoons

Latest episode of Writing Room: http://www.youtube.com/badplanetmusic

HIT ME UP! Twitter: http://www.twitter.com/davedays Facebook: http://www.facebook.com/ddays Merch: http://www.westaspenmerch.com/davedays http://www.davedays.com

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
IamGretar
  • 71
  • 9
  • I've resorted to putting the string into a dummy UILabel and telling the label to resize itself to fit. There are other functions that sound like they'd work, but they don't. – Hot Licks Jul 08 '13 at 10:44
  • 1
    Related: [How do I size a UITextView to its content?](http://stackoverflow.com/q/50467/457406) and [sizeWithFont doesn't give correct height for UITextView if there is a long string in the text being wrapped](http://stackoverflow.com/q/2330939/457406) – Matthias Bauch Jul 08 '13 at 11:00
  • http://stackoverflow.com/questions/6962641/get-the-nsstring-height – iPatel Jul 08 '13 at 11:09

1 Answers1

2

To "measure" text height you could use something like this:

-(CGFloat)measureText:(NSString*)text {  
    CGSize requiredSize = [text sizeWithFont:[UIFont systemFontOfSize:14]  constrainedToSize:CGSizeMake(220, CGFLOAT_MAX)];  
    return requiredSize.height;  
}

The "220" is the maximum width of the text view - change it for your case - and of course you could change the font type/size.

Also, have a look at NSString UIKit Additions Reference for more information.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
tagyro
  • 1,638
  • 2
  • 21
  • 36