嘿伙计们,我需要一些帮助。我有一个 UITextView 的子类,它曾经在 iOS6 下工作。但是在 iOS7 中它有一些奇怪的行为。我重写了 shouldChangeTextInRange 方法,以便可以处理一些键盘输入。以下代码显示了这种奇怪的行为。当您按“a”(或者您可以将“a”替换为您想要的任何其他字符)时,光标将向后跳到 UITextView 中的某个位置,然后返回它应该在的位置。在 iOS6 中,它永远不会显示这样的内容。在 iOS7 中,它不会影响用户的输入,但看起来确实很奇怪。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSMutableString *updatedText = [[NSMutableString alloc] initWithString:textView.text];
[updatedText insertString:text atIndex:range.location];
const char *pch = [text UTF8String];
switch (*pch) {
case 'a': // you can replace it with any other char.
textView.text = updatedText;
return NO;
break;
default:
return YES;
break;
}
}
如果你想看的话,我把这个演示放在一个简单的项目中。 https://dl.dropboxusercontent.com/u/75922069/test-uitextview.zip