1 @interface Viewcontroller () <UITextFieldDelegate>2{3 nsstring * previoustextfieldcontent; 4 Uitextrange *previousselection; 5 }6@end
1 -(void) viewdidload2{3 [Super Viewdidload]; 4 // additional setup after loading the view, typically from a nib. 5 Self.textfield. delegate = self ; 6 [Self.textfield addtarget:self Action: @selector (Formatphonenumber:) forControlEvents: Uicontroleventeditingchanged]; 7 }
1- (void) Formatphonenumber: (uitextfield*) TextField2 {3Nsuinteger targetcursorposition =4 [TextField offsetFromPosition:textField.beginningOfDocument5 ToPosition:textField.selectedTextRange.start];6NSLog (@"Targetcursorposition:%li", (Long) targetcursorposition);7 //Nstr represents a number without spaces8nsstring* nstr = [Textfield.text stringbyreplacingoccurrencesofstring:@" "Withstring:@""];9nsstring* pretxt = [previoustextfieldcontent stringbyreplacingoccurrencesofstring:@" "TenWithstring:@""]; One A CharEditflag =0;//0 When the delete operation is being performed, otherwise 1 - - if(Nstr.length <=pretxt.length) { theEditflag =0; - } - Else { -Editflag =1; + } - + //TextField Setting Text A if(Nstr.length > One) at { -Textfield.text =previoustextfieldcontent; -Textfield.selectedtextrange =previousselection; - return; - } - in //Space -nsstring* Spacestr =@" "; to +nsmutablestring* mstrtemp = [nsmutablestringNew]; - intSpacecount =0; the if(Nstr.length <3&& nstr.length >-1) * { $Spacecount =0;Panax Notoginseng}Else if(Nstr.length <7&& nstr.length >2) - { theSpacecount =1; + A}Else if(Nstr.length < A&& nstr.length >6) the { +Spacecount =2; - } $ $ for(inti =0; i < Spacecount; i++) - { - if(i = =0) { the[Mstrtemp AppendFormat:@"%@%@", [Nstr Substringwithrange:nsmakerange (0,3)], spacestr]; -}Else if(i = =1)Wuyi { the[Mstrtemp AppendFormat:@"%@%@", [Nstr Substringwithrange:nsmakerange (3,4)], spacestr]; -}Else if(i = =2) Wu { -[Mstrtemp AppendFormat:@"%@%@", [Nstr Substringwithrange:nsmakerange (7,4)], spacestr]; About } $ } - - if(Nstr.length = = One) - { A[Mstrtemp AppendFormat:@"%@%@", [Nstr Substringwithrange:nsmakerange (7,4)], spacestr]; + } the - if(Nstr.length <4) $ { the[Mstrtemp appendstring:[nstr substringwithrange:nsmakerange (nstr.length-nstr.length%3, theNstr.length%3)]]; the}Else if(Nstr.length >3) the { -NSString *str = [Nstr substringfromindex:3]; in[Mstrtemp appendstring:[str substringwithrange:nsmakerange (str.length-str.length%4, theStr.length%4)]]; the if(Nstr.length = = One) About { the[Mstrtemp Deletecharactersinrange:nsmakerange ( -,1)]; the } the } +NSLog (@"=======mstrtemp=%@", mstrtemp); - theTextfield.text =mstrtemp;Bayi //TextField Setting Selectedtextrange theNsuinteger curtargetcursorposition = targetcursorposition;//offset position of the current cursor the if(Editflag = =0) - { - //Delete the if(Targetcursorposition = =9|| Targetcursorposition = =4) the { theCurtargetcursorposition = targetcursorposition-1; the } - } the Else { the //Add the if(Nstr.length = =8|| Nstr.length = =3)94 { theCurtargetcursorposition = Targetcursorposition +1; the } the }98 AboutUitextposition *targetposition =[TextField Positionfromposition:[textfield beginningofdocument] - Offset:curtargetcursorposition];101 [TextField Setselectedtextrange:[textfield textrangefromposition:targetposition102 Toposition:targetposition]];103 }104 the-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string106 {107Previoustextfieldcontent =Textfield.text;108Previousselection =Textfield.selectedtextrange;109 the returnYES;111}
IOS Mobile number format, 344 format