我知道这个问题被问过,但我找不到一个有效的答案。结合之前的解决方案,我想出了这段代码:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
{
int numLines = notesTextView.contentSize.height / notesTextView.font.lineHeight;
if (numLines <= 8)
{
return true;
}
return false;
}
这种方法不起作用,因为在添加文本之前已经计算了行数,所以我们仍然会超出我们想要的一行,并且被困在那里,无法进行进一步编辑。
我还尝试过检测"\n"输入的解决方案,但这也不起作用,因为我们可以通过自然换行而达到新的行。