The specified character input for the IOS settings input box

Source: Internet
Author: User

First, only the pure numbers can be entered

Here take Uitextfield as an example: its implementation code is as follows:

123456789101112131415161718 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {    return[self validateNumber:string];}- (BOOL)validateNumber:(NSString*)number {    BOOL res = YES;    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];    inti = 0;    while(i < number.length) {        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];        NSRange range = [string rangeOfCharacterFromSet:tmpSet];        if(range.length == 0) {            res = NO;            break;        }        i++;    }    returnres;}

In addition, we have a simpler way to do this:

First macro definition

1 #define NUMBER @"0123456789"

Then

123456 -  (BOOL) TextField: ( uitextfield *) Textfield shouldchangecharactersinrange: (Nsrange) Range replacementstring: ( nsstring *) string {      nscharacterset *cs = [[nscharacterset  Charactersetwithcharactersinstring:number] invertedset]; &NBSP;&NBSP;&NBSP;&NBSP; nsstring *filtered = [[string componentsseparatedbycharactersinset : cs] componentsjoinedbystring:@ ]; &NBSP;&NBSP;&NBSP;&NBSP; return  [string isequaltostring:filtered];

Enter only uppercase and lowercase letters

Similar to the above only input pure numbers, simple to implement, only need macro definition

1 #define LETTER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

And then implement

123456 -  (BOOL) TextField: ( uitextfield *) Textfield shouldchangecharactersinrange: (Nsrange) Range replacementstring: ( nsstring *) string {      nscharacterset *cs = [[nscharacterset  Charactersetwithcharactersinstring:letter] invertedset]; &NBSP;&NBSP;&NBSP;&NBSP; nsstring *filtered = [[string componentsseparatedbycharactersinset : cs] componentsjoinedbystring:@ ]; &NBSP;&NBSP;&NBSP;&NBSP; return  [string isequaltostring:filtered];

Three, uppercase and lowercase letters and digital combination input

Against the above

1 #define LETTER_NUMBER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

The same truth can be entered into those content if the input can be enumerated, we can be set through the define, the implementation is very simple.

Restrict input to Chinese only

The observer is used here.

12345678910111213141516171819202122232425262728293031323334353637383940414243 - (void)viewDidLoad {    [super viewDidLoad];    _myTextField.delegate = self;    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:_myTextField];}- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];        //过滤非汉字字符    textField.text = [self filterCharactor:textField.text withRegex:@"[^\u4e00-\u9fa5]"];        if(textField.text.length >= 4) {        textField.text = [textField.text substringToIndex:4];            }    returnNO;}- (void)textFiledEditChanged:(id)notification{        UITextRange *selectedRange = _myTextField.markedTextRange;    UITextPosition *position = [_myTextField positionFromPosition:selectedRange.start offset:0];        if(!position) { //// 没有高亮选择的字        //过滤非汉字字符        _myTextField.text = [self filterCharactor:_myTextField.text withRegex:@"[^\u4e00-\u9fa5]"];                if(_myTextField.text.length >= 4) {            _myTextField.text = [_myTextField.text substringToIndex:4];        }    }else{ //有高亮文字        //do nothing    }}- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{    NSString *searchText = string;    NSError *error = NULL;    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];    NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) withTemplate:@""];    returnresult;}- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}

If you want to limit the number of input characters can be directly set, there are many kinds of implementation, the simplest is

123456 -  ( void ) Textviewdidchange: (uitextview *) textview{ &NBSP;&NBSP;&NBSP;&NBSP; nsinteger number = [ Textview.text length]; &NBSP;&NBSP;&NBSP;&NBSP; if   (number >  300 )  {           textview.text = [textview.text substringtoindex: Code class= "AS3 value" >300 ]; &NBSP;&NBSP;&NBSP;&NBSP; } }

The specified character input for the IOS settings input box

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.