I have a UITextView
added on my UIView
. The textview added is not editable, it is just to display some data. The data displayed in the textview is dynamic. Thats is the number of lines is not fixed. It may vary. So if the number of line increases, the size of the textview also needs to be increased. I have no clue how to do this. Please give me some ideas.
UPDATE:
Here's what I'm doing:
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; baseView.backgroundColor = [UIColor grayColor]; [window addSubview:baseView]; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, 30)]; textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; textView.text = @"asdf askjalskjalksjlakjslkasj"; [textView sizeToFit]; [baseView addSubview:textView];
It can be done using the UITextView contentSize . This will not work if auto layout is ON. With auto layout, the general approach is to use the sizeThatFits method and update the constant value on a height constraint. CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.
Step 1 : Click the Attribute Inspector, Select the Border Styles which is not the rounded one. Step 2 : Now go to Size Inspector and change the size of the TextField. Step 3 : Create an @IBOutlet for TextField and then add below code inside the viewWillAppear() or viewDidAppear() .
There is an answer posted at How do I size a UITextView to its content?
CGRect frame = _textView.frame; frame.size.height = _textView.contentSize.height; _textView.frame = frame;
or better(taking into account contentInset thanks to kpower's comment)
CGRect frame = _textView.frame; UIEdgeInsets inset = textView.contentInset; frame.size.height = _textView.contentSize.height + inset.top + inset.bottom; _textView.frame = frame;
note: If you are going to reference a property of an object many times(e.g. frame or contentInset) it's better to assign it to a local variable so you don't trigger extra method calls(_textView.frame/[_textView frame] are method calls). If you are calling this code a lot(100000s of times) then this will be noticeably slower(a dozen or so method calls is insignificant).
However... if you want to do this in one line without extra variables it would be
_textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, _textView.contentSize.height + _textView.contentInset.top + _textView.contentInset.bottom);
at the expense of 5 extra method calls.
You can use setFrame:
or sizeToFit
.
UPDATE:
I use sizeToFit
with UILabel
, and it works just fine, but UITextView
is a subclass of UIScrollView
, so I can understand why sizeToFit
doesn't produce the desired result.
You can still calculate the text height and use setFrame
, but you might want to take advantage of UITextView
's scrollbars if the text is too long.
Here's how you get the text height:
#define MAX_HEIGHT 2000 NSString *foo = @"Lorem ipsum dolor sit amet."; CGSize size = [foo sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, MAX_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
and then you can use this with your UITextView
:
[textView setFont:[UIFont systemFontOfSize:14]]; [textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
or you can do the height calculation first and avoid the setFrame
line:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, size.height + 10)];
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