Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set height of UITextView according to its content in objective c

I have a UITextView and want to set its height as per its content changes.

How can I get height of the content.

Please guide me.

Thanks in advance.

like image 228
Shivomkara Chaturvedi Avatar asked Sep 13 '11 11:09

Shivomkara Chaturvedi


3 Answers

If you want to calculate height of your UITextView for some text then you can use such method:

CGSize textViewSize = [text sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:20] 
                       constrainedToSize:CGSizeMake(WIDHT_OF_VIEW, FLT_MAX) 
                       lineBreakMode:UILineBreakModeTailTruncation];

In fontWithName define font that you are using in UITextView, in size parameter - size of your text in UITextView. Replace WIDHT_OF_VIEW width width of your UITextView.

textViewSize.height will contain height that UITextField to display text without scrolling

like image 87
Nekto Avatar answered Nov 15 '22 06:11

Nekto


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect frame = _textView.frame;
    frame.size.height = _textView.contentSize.height;
    _textView.frame = frame;
}
like image 37
alloc_iNit Avatar answered Nov 15 '22 06:11

alloc_iNit


You can calculate the size of the text with the method from UIStringDrawing category:

CGSize textSize = [textView.text sizeWithFont:font 
                                 forWidth:textWidth 
                                 lineBreakMode: UILineBreakModeWordWrap];
like image 38
Davyd Geyl Avatar answered Nov 15 '22 08:11

Davyd Geyl