views:

5761

answers:

2

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];
+3  A: 

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)];
Can Berk Güder
I already tried sizeToFit. It doesn't make any change
saikamesh
actually I don't know how to delete this answer and how to update my own question. There is no option for doing these. (FYI im using safari browser)
saikamesh
sorry! just now I saw the options for those
saikamesh
this works fine. but it needs the frame to be set initially. that is before ur piece of code I added textView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 30.0, 100, 30)];.
saikamesh
after adding the above only the textview becomes visible. and the last line in the text view is not displayed fully. only the upper half of it gets displayed.
saikamesh
any clue to solve this issue (only the upper half of last line gets displayed)
saikamesh
That's because the UITextView itself is larger than the text. Just replace size.height with size.height + 10 or something.
Can Berk Güder
solution you gave works fine. thanks a lot. but please tell me why UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 30.0, 100, 30)] needs to be given at first
saikamesh
Well, you have to initialize textView to something. But you can do the height calculation first and getRid of the setFrame line.
Can Berk Güder
oh, and don't forget to accept my answer if it worked for you.
Can Berk Güder
it works fine. but I have no idea how to accept your answer. is that "link" I need to click on?
saikamesh
=) you should see a checkmark next to my answer.
Can Berk Güder
+1  A: 

There is an answer posted at http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
Gabe