UITextField specifies the number of Chinese characters to be entered. uitextfield
As I wrote earlier, UITextField Category is used to limit the number of words to be entered. It is a Bug that crash is used to input Chinese characters. Now the code is modified as follows:
. H file
# Import <UIKit/UIKit. h> @ interface UITextField (LimitLength)/*** you only need to call this method and add a length (int) to limit the number of words, chinese characters cannot be ** @ param length */-(void) limitTextLength :( int) length;/*** uitextField jitter effect */-(void) shake; @ end
. M file
# Import "UITextField + LimitLength. h "# import <objc/runtime. h> @ implementation UITextField (LimitLength) static NSString * kLimitTextLengthKey = @ "kLimitTextLengthKey";-(void) limitTextLength :( int) length {detail (self, (_ bridge const void *) (kLimitTextLengthKey), [NSNumber numberWithInt: length], OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self addTarget: self action: @ selector (textFieldTextLengt HLimit :) forControlEvents: UIControlEventEditingChanged];}-(void) handle :( id) sender {NSNumber * lengthNumber = handle (self, (_ bridge const void *) (kLimitTextLengthKey )); int length = [lengthNumber intValue]; // The following is the modified part of bool isChinese; // determine whether the current input method is Chinese NSArray * currentar = [UITextInputMode activeInputModes]; UITextInputMode * current = [currentar firstObject]; // [UIT ExtInputMode currentInputMode] primaryLanguage], the discarded method if ([current. primaryLanguage isChinese tostring: @ "en-US"]) {isChinese = false;} else {isChinese = true;} if (sender = self) {// length is the number of digits set by myself NSString * str = [[self text] stringByReplacingOccurrencesOfString :@"? "WithString: @" "]; if (isChinese) {// UITextRange * selectedRange = [self markedTextRange] in the Chinese Input Method; // obtain the highlighted UITextPosition * position = [self positionFromPosition: selectedRange. start offset: 0]; // if (! Position) {if (str. length >=length) {NSString * strNew = [NSString stringWithString: str]; [self setText: [strNew substringToIndex: length];} else {// NSLog (@ "input") ;}} else {if ([str length]> = length) {NSString * strNew = [NSString stringWithString: str]; [self setText: [strNew substringToIndex: length] ;}}}- (void) shake {CAKeyframeAnimation * keyAn = [CAKeyframeAnimation animationWithKeyPath: @ "position"]; [keyAn setDuration: 0.5f]; NSArray * array = [[NSArray alloc] initWithObjects: [NSValue valueWithCGPoint: CGPointMake (self. center. x, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x-5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x + 5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x-5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x + 5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x-5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x + 5, self. center. y)], [NSValue valueWithCGPoint: CGPointMake (self. center. x, self. center. y)], nil]; [keyAn setValues: array]; NSArray * times = [[NSArray alloc] initWithObjects: [NSNumber numberWithFloat: 0.1f], [NSNumber numberWithFloat: 0.2f], [NSNumber resume: 0.3f], [NSNumber numberWithFloat: 0.4f], [NSNumber numberWithFloat: 0.5f], [NSNumber resume: 0.6f], [NSNumber resume: 0.7f], [NSNumber numberWithFloat: 0.8f], [NSNumber numberWithFloat: 0.9f], [NSNumber numberWithFloat: 1.0f], nil]; [keyAn setKeyTimes: times]; [self. layer addAnimation: keyAn forKey: @ "TextAnim"] ;}@ end
Reference: http://www.keepwhy.com/index.php/archives/296
Http://blog.sina.com.cn/s/blog_60f977e70101g4gj.html
Http://www.2cto.com/kf/201406/312073.html
The input box limits the number of words entered.
Press F12 to find maxlength = "64" in the code and then enter
Limit the number of words entered in multiple text boxes
Html already written. Try it by yourself.
<Textarea name = "textfield2" onkeydown = "if (event. srcElement. value. length> 50) return false;"> </textarea>
----------------------------------------------------------
Add attributes in each text box. I can limit any text box!
Onkeydown = "if (event. srcElement. value. length> 50) return false ;"
----------------------
Capuchin is a fool. Can textarea use the maxlength = 16 attribute?