美文网首页
IOS UITextField 内容格式化

IOS UITextField 内容格式化

作者: 冰冰凉3 | 来源:发表于2018-11-16 11:49 被阅读0次

    电话号码,银行卡号等内容格式化

    途径:通过UITextFiled 的delegate实现

    废话少说,上代码

    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        /* KeyPoint:
         1. string 为正要输入的内容(1个字符,或者联想字符)
         2. text 为已经校验完成的内容
         */
        NSString *linkCharacter = @" ";
        NSInteger maxLength = 14;   //=连接符数+字符数+1
        NSInteger firstSectionLength = 3;
        NSInteger sectionLength = 4;
        
        switch (textField.tag) {
            case 101:
                maxLength = 20;
                firstSectionLength = 4;
                break;
            case 103:
                linkCharacter= @"/";
                maxLength = 6;
                firstSectionLength= 2;
                sectionLength =4;
                break;
            case 109:
                maxLength = 14;
                firstSectionLength= 3;
                sectionLength = 4;
                break;
            default:
                break;
        }
        
        
        NSString *text = [textField text];
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        
        /*剔除输入的空格字符*/
        string = [string stringByReplacingOccurrencesOfString:linkCharacter withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            /*输入包含了非法字符,直接返回*/
            return NO;
        }
        
        /*在光标位置插入字符*/
        text = [text stringByReplacingCharactersInRange:range withString:string];
        /*剔除连接符号*/
        text = [text stringByReplacingOccurrencesOfString:linkCharacter withString:@""];
    
        /*添加连接符*/
        NSString *newString = @"";
        NSString *subString = [text substringToIndex:MIN(text.length, firstSectionLength)];
    
        
        newString = [newString stringByAppendingString:subString];
    
        if(subString.length == firstSectionLength){
            newString = [newString stringByAppendingString:linkCharacter];
        }
        text = [text substringFromIndex:MIN(text.length, firstSectionLength)];
        
        while (text.length > 0) {
            NSString *subString2 = [text substringToIndex:MIN(text.length, sectionLength)];
            newString = [newString stringByAppendingString:subString2];
            if (subString2.length == sectionLength) {
                newString = [newString stringByAppendingString:linkCharacter];
            }
            text = [text substringFromIndex:MIN(text.length, sectionLength)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    
        if (newString.length >= maxLength) {
            return NO;
        }
        [textField setText:newString];
    
        return NO;
    }
    
    
    

    解释:传入的值为textField,如果有多个textfield,需要对其进行编号(通过tag设定),然后根据tag值进行分类,如何进行格式化

    格式化参数(可以通过新建的HBTextFieldFormat类进行统一管理):

    1. 最大长度
    2. 分割连接符号
    3. 第一段长度(手机号第一段长度为3)
    4. 其余section的长度
    
    //.h文件
    
    
    #import <Foundation/Foundation.h>
    
    @interface HBTextFiledFormatter : NSObject
    
    @property(nonatomic,copy)   NSString    *linkCharacter;
    @property(nonatomic,assign) NSInteger    maxlength;
    @property(nonatomic,assign) NSInteger    firstSectionLength;
    @property(nonatomic,assign) NSInteger    sectionLength;
    @end
    
    
    
    
    
    //.m文件
    
    #import "HBTextFiledFormatter.h"
    
    @implementation HBTextFiledFormatter
    -(instancetype)init{
        self = [super init];
        if(self){
            _linkCharacter = @" ";
            _maxlength = 20;
            _firstSectionLength = 4;
            _sectionLength = 4;
        }
        return self;
    }
    @end
    
    

    //采用分类之后的代码如下:

    根据textfield的tag,设定textFieldFormatter的属性值
    -(HBTextFiledFormatter *)textFieldFormatterWithTag:(NSInteger)textFiledTag{
        HBTextFiledFormatter *formatter = [[HBTextFiledFormatter alloc]init];
        switch (textFiledTag) {
            case 101:  //信用卡卡号
                formatter.maxlength = 20;
                formatter.firstSectionlength = 4;
                break;
            case 103: //信用卡的有效期
                formatter.linkCharacter= @"/";
                formatter.maxlength = 6;
                formatter.firstSectionlength= 2;
                formatter.sectionlength =4;
                break;
            case 109: //手机号
                formatter.maxlength = 14;
                formatter.firstSectionlength= 3;
                formatter.sectionlength = 4;
                break;
            default:
                break;
        }
        return formatter;
    }
    
    改编后的delegate Selector
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        /* KeyPoint:
         1. string 为正要输入的内容(1个字符,或者联想字符)
         2. text 为已经校验完成的内容
         */
        _textFieldFormatter = [self textFieldFormatterWithTag:textField.tag];
        NSString *linkCharacter = _textFieldFormatter.linkCharacter;
        NSInteger maxLength = _textFieldFormatter.maxlength;
        NSInteger firstSectionLength = _textFieldFormatter.firstSectionlength;
        NSInteger sectionLength = _textFieldFormatter.sectionlength;
        NSString *text = [textField text];
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        
        
        /*剔除输入的空格字符*/
        string = [string stringByReplacingOccurrencesOfString:linkCharacter withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            /*输入包含了非法字符,直接返回*/
            return NO;
        }
        
        /*在光标位置插入字符*/
        text = [text stringByReplacingCharactersInRange:range withString:string];
        /*剔除连接符号*/
        text = [text stringByReplacingOccurrencesOfString:linkCharacter withString:@""];
    
        /*添加连接符*/
        NSString *newString = @"";
        NSString *subString = [text substringToIndex:MIN(text.length, firstSectionLength)];
    
        
        newString = [newString stringByAppendingString:subString];
    
        if(subString.length == firstSectionLength){
            newString = [newString stringByAppendingString:linkCharacter];
        }
        text = [text substringFromIndex:MIN(text.length, firstSectionLength)];
        
        while (text.length > 0) {
            NSString *subString2 = [text substringToIndex:MIN(text.length, sectionLength)];
            newString = [newString stringByAppendingString:subString2];
            if (subString2.length == sectionLength) {
                newString = [newString stringByAppendingString:linkCharacter];
            }
            text = [text substringFromIndex:MIN(text.length, sectionLength)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    
        if (newString.length >= maxLength) {
            return NO;
        }
        [textField setText:newString];
        return NO;
    }
    

    相关文章

      网友评论

          本文标题:IOS UITextField 内容格式化

          本文链接:https://www.haomeiwen.com/subject/hzapfqtx.html