編集可能なUITextViewを使用するときに入力内容にあわせてUITextViewの高さを変えたい時がある。
■コード
以下のようにUITextViewを定義する。
textview = [[UITextView alloc] init]; [textview setFrame:CGRectMake(0, 0, 100, 50)]; [textview setEditable:YES]; [textview setDelegate:self];
以下のようにcontentSizeを使用する。
-(void)textViewDidChange:(UITextView *)textView { CGRect f = textview.frame; f.size.height = textview.contentSize.height; textview.frame = f; }