UITextField specifies the number of Chinese characters to be entered. uitextfield

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.