在我的应用程序中,我需要限制用户仅在 UITextView 中输入 7 行。当他尝试在 8 行中输入文本时,我们需要停止允许编辑并显示警告消息。它通过使用获取 UITextView 文本的 CGSize 工作正常。
但是当用户在 UITextView 中输入文本作为粘贴时,如果文本超过 7 行,则不允许。根据要求,我需要从输入(复制和粘贴)中获取 7 行文本到 UITextView。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
{
NSString *temp = [textView.text stringByReplacingCharactersInRange:range withString:string]
CGSize size = [temp sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width,999) lineBreakMode:UILineBreakModeWordWrap];
int numLines = size.height / textView.font.lineHeight;
if (numLines <= 8)
{
return true;
}
//Alert
return false;
}
请在这个问题上帮助我。提前致谢。