美文网首页
UItextField 手机号切割

UItextField 手机号切割

作者: 40dd4b561abe | 来源:发表于2017-12-21 11:35 被阅读37次

    用这个 UIControlEventEditingChanged 事件绑定

    
    #pragma  mark - textField -代理
    - (void)textFieldDidChange:(UITextField *)text
    {
        NSLog(@"%@",text.text);
        NSString * str = [text.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        if (str.length >3&&text.text.length >3) {
            text.text = [NSString stringWithFormat:@"%@ %@",[str substringWithRange:NSMakeRange(0, 3)],[str substringWithRange:NSMakeRange(3,str.length - 3)]];
        }
        if (text.text.length == 4 &&str.length ==3) {
            text.text = [NSString stringWithFormat:@"%@",[str substringWithRange:NSMakeRange(0, 3)]];
        }
        if (str.length >7&&text.text.length >7) {
            text.text = [NSString stringWithFormat:@"%@ %@",[text.text substringWithRange:NSMakeRange(0, 8)],[text.text substringWithRange:NSMakeRange(8, text.text.length - 8)]];
        }
        if (str.length >11) {
            text.text = [text.text substringWithRange:NSMakeRange(0, 13)];
        }
    //    if (text.text.length == 8 &&str.length ==7) {
    //        text.text = [NSString stringWithFormat:@"%@",[str substringWithRange:NSMakeRange(0, 7)]];
    //    }
    }
    

    相关文章

      网友评论

          本文标题:UItextField 手机号切割

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