(轉(zhuǎn))iOS-設(shè)置UITextfield內(nèi)手機(jī)號(hào)碼按344格式顯示

原文:http://www.itdecent.cn/p/c97f9206226c

    [phoneTF addTarget:self action:@selector(phoneNumChange:) forControlEvents:UIControlEventEditingChanged]; // 監(jiān)聽改變

定義屬性

NSString *previousTextFieldContent;
    UITextRange *previousSelection;

監(jiān)聽方法 和 代理方法

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    previousTextFieldContent = textField.text;
    previousSelection = textField.selectedTextRange;
return YES;
}

-(void)phoneNumChange:(id)sender{


    UITextField *textField = (UITextField *)sender;

    //限制手機(jī)賬號(hào)長度13位(有兩個(gè)空格)
    if (textField.text.length > 13) {
        textField.text = [textField.text substringToIndex:13];
    }

    NSUInteger targetCursorPosition = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];

    NSString *currentStr = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *preStr = [previousTextFieldContent stringByReplacingOccurrencesOfString:@" " withString:@""];

    //正在執(zhí)行刪除操作時(shí)為0,否則為1
    char editFlag = 0;
    if (currentStr.length <= preStr.length) {
        editFlag = 0;
    }else{
        editFlag = 1;
    }


    NSMutableString *tempStr = [NSMutableString new];

    int spaceCount = 0;
    if (currentStr.length < 3 && currentStr.length > -1) {
        spaceCount = 0;
    }else if (currentStr.length < 7 && currentStr.length > 2) {
        spaceCount = 1;
    }else if (currentStr.length < 12 && currentStr.length > 6) {
        spaceCount = 2;
    }

    for (int i = 0; i < spaceCount; i++) {
        if (i == 0) {
            [tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(0, 3)], @" "];
        }else if (i == 1) {
            [tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(3, 4)], @" "];
        }else if (i == 2) {
            [tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(7, 4)], @" "];
        }
    }

    if (currentStr.length == 11) {
        [tempStr appendFormat:@"%@%@", [currentStr substringWithRange:NSMakeRange(7, 4)], @" "];
    }
    if (currentStr.length < 4) {
        [tempStr appendString:[currentStr substringWithRange:NSMakeRange(currentStr.length - currentStr.length % 3, currentStr.length % 3)]];
    }else if(currentStr.length > 3 && currentStr.length <12) {
        NSString *str = [currentStr substringFromIndex:3];
        [tempStr appendString:[str substringWithRange:NSMakeRange(str.length - str.length % 4, str.length % 4)]];
        if (currentStr.length == 11) {
            [tempStr deleteCharactersInRange:NSMakeRange(13, 1)];
        }
    }
    textField.text = tempStr;
    // 當(dāng)前光標(biāo)的偏移位置
    NSUInteger curTargetCursorPosition = targetCursorPosition;

    if (editFlag == 0) {
        //刪除
        if (targetCursorPosition == 9 || targetCursorPosition == 4) {
            curTargetCursorPosition = targetCursorPosition - 1;
        }
    }else {
        //添加
        if (currentStr.length == 8 || currentStr.length == 4) {
            curTargetCursorPosition = targetCursorPosition + 1;
        }
    }
    UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] offset:curTargetCursorPosition];
    [textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition toPosition :targetPosition]];

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,656評(píng)論 1 32
  • {"keys":[{"key2":"余華","key":"活著","url":"https://www.jians...
    書籍?dāng)?shù)據(jù)記錄閱讀 698評(píng)論 0 0
  • 13個(gè)小技巧幫你征服Xcode: http://www.cocoachina.com/ios/20150428/1...
    Stago閱讀 985評(píng)論 0 1
  • 主編:張逸驍 【班級(jí)新聞】 【一句話新聞】 1. 本周周三到周五,學(xué)校每天都在訓(xùn)練大家廣播體操,同學(xué)們做操認(rèn)真...
    青春飛揚(yáng)358閱讀 2,164評(píng)論 0 5
  • 每天來簡書里做更新,其實(shí)有時(shí)候我真的不知道寫什么好,但是又不想就這樣放棄了,每天寫點(diǎn)東西是給自己的任務(wù)也是想堅(jiān)持下...
    都市和尚閱讀 168評(píng)論 0 0

友情鏈接更多精彩內(nèi)容