我想检测UITextView
中键盘上的空格点击事件中删除了哪个字符。
所以,如果有人知道解决方案,请帮忙。 提前谢谢。
答案 0 :(得分:1)
你应该实现协议UITextViewDelegate方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
只需获取范围内的文字 试试这个有一个日志(它是空的,以防它不是替代品而是一个新的输入)
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"deleting this string: |%@|", [textView.text substringWithRange:range]);
return YES;
}
PS
记得将您的类设置为UITextView的委托
答案 1 :(得分:-1)
这就是你想要的。此方法会在删除角色时检测,并将其打印到控制台。
@interface ViewController ()<UITextFieldDelegate>
{
UITextField *textField;
NSString *currentText;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 30)];
[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
}
-(void)textChanged:(UITextField *)sender{
if (!currentText) {
currentText = sender.text;
}
if (![currentText isEqualToString:sender.text]) {
//The text that is in the textField at the moment is shorter than it was the last time the textfield was editted... This shows that a backspace was pressed
if (currentText.length > sender.text.length) {
NSLog(@"Character: %@", [currentText substringFromIndex:sender.text.length]);
}
}
currentText = sender.text;
}
@end