UITextfield有这样一个属性markedTextRange:
英文解释:
Discussion
If there is no marked text, the value of the property is nil. Marked text is provisionally inserted text that requires user confirmation; it occurs in multistage text input. The current selection, which can be a caret or an extended range, always occurs within the marked text.
翻译:
如果没有标记的文本,属性的值为nil。标记文本是需要用户确认的临时插入文本; 它出现在多级文本输入中。当前选择(可以是插入符号或扩展范围)始终位于标记的文本中。
注释:所谓的标记文本指的是下图,并且在英文状态下markedTextRange为nil。
我们只需要在UITextFieldTextDidChangeNotification之后,做文本截取即可。
self.myTF.text = [self.myTF.text substringToIndex:MAX_LENGTH];