Do you want to enter Chinese in the end?
Of course to find, entered the pinyin has not selected the Chinese character mark!
What the? Do you doubt that there will be no sign?
Indeed I have had this kind of doubt, because looked for Uitextfield, completely has no clue
There's no uitextfield, it's got to be associated with it.
All right, no, you're not in circles.
Uitextfield's There is such a property in the Uitextinput protocol Markedtextrange
@property (nonatomic, readonly) uitextrange *markedtextrange; //Nil If no highlighted text
The final code for the previous article was 3. This is the basis for optimization, which is repeated here once
3.-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{ if (range.location >=) { return NO; } return YES;}
Goal: Enter Chinese characters in the last position to break the limit
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{ && (textfield.markedtextrange = = Nil)) { return NO; } return YES;}
Can indeed exceed the limit, but did not delete, do not worry, there is a way to add a judgment logic
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string{ if&& range.length = = 0)) { return NO; } return YES;}
. length = = 0, which means more input,. length = = 1 means delete
This way, the problem comes again, when the choice of Chinese characters, the word count may have exceeded the maximum number of words, so also need to add some logic
#define MaxNumber 10
[Self.field addtarget:self Action: @selector (Textfielddidchange:) forcontrolevents:uicontroleventeditingchanged];
-(void) Textfielddidchange: (Uitextfield *) textfield{ if (textfield.markedtextrange = = nil) { if ( TextField.text.length > MaxNumber) { textfield.text = [Textfield.text substringtoindex:maxnumber];}} } -(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{ maxnumber && (textfield.markedtextrange = nil && range.length = 0)) { C7/>return NO; } return YES;}
Since then, has been able to solve the problem of Chinese input restrictions, said so much in the end these are the final ^_^ more need to solve their own
iOS limit text input length step two