[self.rightTF addTarget:self action:@selector(rightTFValueChange:) forControlEvents:UIControlEventEditingChanged]; -(void)rightTFValueChange:(UITextField *)textField{ UITextRange *selectedRange = [self.rightTF markedTextRange]; NSString * newText = [self.rightTF textInRange:selectedRange]; //获取高亮部分 if(newText.length>0) return; //不允许输入空格 self.rightTF.text = [self.rightTF.text stringByReplacingOccurrencesOfString:@" " withString:@""]; if (self.rightTF == textField) {//文本框最大长度 textField.text = textField.text.length > 16 ? [textField.text substringToIndex:16] : textField.text; } self.writeStr(self.rightTF.text); }