This should be so easy, but for some reason it's not working for me. I want to resize my text box and ideally have some padding at the bottom. For now I'll settle for resizing properly.

The setup

A single view on my storyboard

Interface

@property (weak, nonatomic) IBOutlet UITextView *messageTextBox;

Implementation

- (void)viewDidLoad
{
    [super viewDidLoad];
    _messageTextBox.text = _message.body;
    [_messageTextBox sizeToFit];  
}

I have tried several different solutions including changing the height of the frame and other user suggestions here...

This is using ios7 and xcode 5, but I don't see why there would be an issue with such a basic function.

To add to my confusion, I have logged the frame height and contentsize after sizeToFit. My results are:

2013-10-02 18:40:20.803 narg[21467:a0b] H of the frame: 618.000000
2013-10-02 18:40:20.804 narg[21467:a0b] W of the frame: 469.000000
2013-10-02 18:40:20.804 narg[21467:a0b] Reported H of contentSize property on object 42.000000

So the frame clearly knows the correct height, but on the display all the text is cutoff and the box doesn't adjust size.

有帮助吗?

解决方案

Here was the answer to my issue. At some point I had checked off Autolayout on my view. This effectively locked the view and didn't allow me to adjust the frame size.

If you are seeing a similar issue that the frame is not resizing try the following:

  • Go to interface builder and open the view
  • Click on the file inspector
  • Deselect auto-layout.

This resolved my issue.

其他提示

Have a look here to calculate the good frame of the UITextView depending of your text _message.body. You can add a gap to have a bottom padding like this :

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width
{
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:text];
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height + 50; // 5O is your gap for example
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top