开发者

UITextView contentSize.height larger than necessary?

开发者 https://www.devze.com 2023-03-23 21:21 出处:网络
UITextView scrolls even when all the text fits within its frame. It scrolls as if there are two new lines at the bottom of the text, but there aren\'t. So, its contentSize.height seems to be a couple

UITextView scrolls even when all the text fits within its frame. It scrolls as if there are two new lines at the bottom of the text, but there aren't. So, its contentSize.height seems to be a couple lines开发者_高级运维 larger than the text. How do I get rid of this? I.e., how do I tighten up the content size?


You can set up the contentSize for that UITextView so that it tightens to the text.

You can make use of NSString methods named sizeWithFont.... You can check full list here: http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

I've noticed some issues working with some of them, but one that never fails me is:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

I suppose you have a fixed width and want to find out the correct height. Considering a fixed 200px width, it would be something like:

NSString *myText = @"Some text...";
CGSize tightContentSize = [mytext sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:CGSizeMake(200, 10000000)].height;

[myTextView setContentSize:tightContentSize];

Take into consideration that your UITextView has an 8px padding in all directions, so if your UITextView frame is 200 width, you would actualy have a 184px width for text.


It's late but, for other people, try with:

self.textView.contentInset = UIEdgeInsetsZero;

That's work for me.

0

精彩评论

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

关注公众号