开发者

Setting the UITextView height to fit the content height in Objective c

I have the code as below for my UITextView set up.

CGRect itemDescFrame = CGRectMake(20, 160, 280, 200);
UITextView *itemDesc = [[UITextView alloc] initWithFrame:itemDescFrame];
...
itemDesc.editable = NO;
itemDesc.scrollEnabled = NO;
...

I have disabled the UITextView scroll bar, however some of the content on the bottom is being hidden. How can I set the UITextView height to fit to the content height?

Thanks

UPDATE:

NSLog(@"frame %@", itemDesc.frame.size.height);
NSLog(@"content %@", itemDesc.contentSize.height);

Result:

2011-10-11 11:00:18.010 abc[1606:207] frame 200.000000
2011-10-开发者_StackOverflow社区11 11:00:18.010 abc[1606:207] content 200.000000


As a subclass of UIScrollView, UITextView has a property called contentSize which, in the case of a UITextView, should be the size that the text occupies after it is is placed in the view. Try setting the height of the text view's frame to the height of contentSize.


Simply add this line of code to force the frame to update its size based on the height of the text it contains:

[self.myTextView sizeToFit];

Note that if you want to add additional padding you can set textContainerInset.

You'll want to call that when the text content changes and upon rotation. To make it easy to see the height adjust, set a background color on the UITextView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜