iOS开发textfield的一些方法汇总

1,动态获得textfield 的输入内容

在- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string添加如下代码就可以获得全部的代码。

 NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

慢慢添加吧 

2,//只允许数字和小数点输入

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSString *allText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    

    NSUInteger lengthOfString = string.length;

    if ([string isEqualToString:@"."]) {

        return YES;

    }

    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入

        unichar character = [string characterAtIndex:loopIndex];

        if (character < 48){

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"只允许输入数字位数" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alert show];

            return NO; // 48 unichar for 0

        }

        if (character > 57){

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"只允许输入数字位数" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alert show];

            return NO; // 57 unichar for 9

        }

    }

    return YES;

}

原文地址:https://www.cnblogs.com/godlovexq/p/5054697.html