【发布时间】:2011-02-07 11:38:28
【问题描述】:
我正在尝试构建一个支持可变大小文本框的应用程序,具体取决于内容。 UITextView 的大小应该增加到 4 行,然后激活滚动。
输入文本时我无法获取行数。
请建议我一些方法来做到这一点。任何示例代码 sn-p 将不胜感激。
提前致谢!!
【问题讨论】:
标签: iphone-sdk-3.0 uiscrollview uitextview expandable uitextviewdelegate
我正在尝试构建一个支持可变大小文本框的应用程序,具体取决于内容。 UITextView 的大小应该增加到 4 行,然后激活滚动。
输入文本时我无法获取行数。
请建议我一些方法来做到这一点。任何示例代码 sn-p 将不胜感激。
提前致谢!!
【问题讨论】:
标签: iphone-sdk-3.0 uiscrollview uitextview expandable uitextviewdelegate
我尝试了一个临时解决方案来解决这个问题。可以在视图控制器的 (void)textViewDidChange:(UITextView *)textView 方法中添加以下代码,并将委托更改为 IB 中的文件所有者。
float adjustvar;
if ([textView.text isEqualToString:@""]) {
//change these values according to your requirement as they are hard coded to adjust cursor and size of the textview
adjustvar = 37.0;
textView.contentInset = UIEdgeInsetsMake(6 ,0 ,0 ,0);
}
else {
adjustvar = textView.contentSize.height;
}
CGRect temp = textView.frame;
temp.origin.y = textView.frame.origin.y + textView.frame.size.height - adjustvar;
temp.size.height = adjustvar;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationsEnabled:YES];
if (temp.size.height > 142.0) {
textView.scrollEnabled = YES;
}
else {
textView.scrollEnabled = NO;
textView.frame = temp;
}
[UIView commitAnimations];`
希望这会有所帮助。如果有更好的解决方案,请提供帮助。
谢谢
【讨论】: