【发布时间】:2011-02-06 07:44:06
【问题描述】:
尝试创建一个固定大小的大文本框。
这个问题和140个字符的约束问题很相似,但是不是在140个字符处停止输入,而是想在到达textView的frame的边缘时停止输入,而不是向下延伸到深渊。这是我为委托方法所拥有的。似乎总是有点偏离。有什么想法吗?
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
BOOL edgeBump = NO;
CGSize constraint = textView.frame.size;
CGSize size = [[textView.text stringByAppendingString:text] sizeWithFont:textView.font
constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
if (height > textView.frame.size.height) {
edgeBump = YES;
}
if([text isEqualToString:@"\b"]){
return YES;
} else if(edgeBump){
NSLog(@"EDGEBUMP!");
return NO;
}
return YES;
}
编辑:根据 Max 在下面的建议,这里是有效的代码(但是请注意,自动更正和剪切在这里不起作用):
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGSize constraint = textView.frame.size;
NSString *whatWasThereBefore = textView.text;
textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text];
if (textView.contentSize.height >= constraint.height) {
textView.text = whatWasThereBefore;
}
return NO;
}
【问题讨论】:
标签: iphone objective-c cocoa-touch uitableview