TextField and textView restrict input conditions. textfieldtextview

Source: Internet
Author: User

TextField and textView restrict input conditions. textfieldtextview

The following two methods are used to implement the proxy methods of textfield and textView.

I. textField only supports entering numbers and decimal places, and only up to five digits can be reserved after the decimal point.

-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {// the value in the input box can only be two if (string) digits after the decimal point. length = 0) {return YES;} const char * ch = [string cStringUsingEncoding: NSUTF8StringEncoding]; if (* ch = 0) return YES; if (* ch! = 46 & (* ch <48 | * ch> 57) return NO; if ([textField. text rangeOfString :@". "]. length = 1) {NSUInteger length = [textField. text rangeOfString :@". "]. location; if ([textField. text substringFromIndex: length]> 2 | * ch = 46) return NO;} else {NSInteger existedLength = textField. text. length; NSInteger selectedLength = range. length; NSInteger replaceLength = string. length; if (existedLength-selec TedLength + replaceLength> 5 &&! [String isEqualToString: @ "."]) {return NO;} return YES ;}

2. textView can contain up to 25 words

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if ([text isEqualToString:@""] && range.length > 0) {        return YES;    }else {        if (textView.text.length - range.length + text.length > 25) {            return NO;        }else {            return YES;        }    }}

 

Related Article

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.