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];
You can use
setFrame:orsizeToFit.UPDATE:
I use
sizeToFitwithUILabel, and it works just fine, butUITextViewis a subclass ofUIScrollView, so I can understand whysizeToFitdoesn’t produce the desired result.You can still calculate the text height and use
setFrame, but you might want to take advantage ofUITextView‘s scrollbars if the text is too long.Here’s how you get the text height:
and then you can use this with your
UITextView:or you can do the height calculation first and avoid the
setFrameline: