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.
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
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
}
You can calculate the size of the text with the method from UIStringDrawing category:
CGSize textSize = [textView.text sizeWithFont:font
forWidth:textWidth
lineBreakMode: UILineBreakModeWordWrap];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With