objective c - How to format UITextField with NSNumberFormatter? -
hope helps others, since haven't seen similar post on web shows how format text field using nsnumberformatter
@ same time keep uitextfield
cursor position should naturally be. those, because after formatting, nsstring
inside uitextfield
, setting field
end cursor placed end of field
. nice convert swift needs it.
and here answer issue, using uikeyboardtypenumberpad, work fine uikeyboardtypedecimalpad, if other keyboard types used, feel free add regex before using next code:
- (int)getcharoccurencies:(nsstring *)character instring:(nsstring *)string{ nsmutablearray *characters = [[nsmutablearray alloc] initwithcapacity:[string length]]; (int i=0; < [string length]; i++) { nsstring *ichar = [nsstring stringwithformat:@"%c", [string characteratindex:i]]; [characters addobject:ichar]; } int count = 0; (nsstring *ichar in characters) { if ([ichar isequaltostring:character]) { count++; } } return count; } - (void)selecttextforinput:(uitextfield *)input atrange:(nsrange)range { uitextposition *start = [input positionfromposition:[input beginningofdocument] offset:range.location]; uitextposition *end = [input positionfromposition:start offset:range.length]; [input setselectedtextrange:[input textrangefromposition:start toposition:end]]; } - (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{ nsstring *charusedtoformat = @","; nsmutablestring *textstring = [nsmutablestring stringwithstring:[textfield text]]; [textfield settintcolor:[uicolor darkgraycolor]]; if ([string isequaltostring:charusedtoformat]) { return no; } if (range.location == 0 && [string isequaltostring:@"0"]) { return no; } if ([[textstring stringbyreplacingcharactersinrange:range withstring:string] length] == 0) { textfield.text = @""; [self selecttextforinput:textfield atrange:nsmakerange(0, 0)]; return no; } nsstring *replaceblestring = [textstring substringwithrange:range]; if (string.length == 0 && [replaceblestring isequaltostring:charusedtoformat]) { nsrange newrange = nsmakerange( range.location - 1, range.length); range = newrange; textstring = [nsmutablestring stringwithstring:[textstring stringbyreplacingcharactersinrange:newrange withstring:string]]; }else{ textstring = [nsmutablestring stringwithstring:[textstring stringbyreplacingcharactersinrange:range withstring:string]]; } int commmacountbefore = [self getcharoccurencies:charusedtoformat instring:textstring]; textstring = [nsmutablestring stringwithstring:[textstring stringbyreplacingoccurrencesofstring:charusedtoformat withstring:@""]]; nsnumber *firstnumber = [nsnumber numberwithdouble:[textstring doublevalue]]; nsnumberformatter *formatter = [[nsnumberformatter alloc] init]; [formatter setnumberstyle:nsnumberformatterdecimalstyle]; //just in case [formatter setformatterbehavior:nsnumberformatterbehavior10_4]; [formatter setgroupingseparator:charusedtoformat]; [formatter setdecimalseparator:@""]; [formatter setmaximumfractiondigits:0]; textstring = [nsmutablestring stringwithstring:[formatter stringforobjectvalue:firstnumber]]; textfield.text = textstring; int commmacountafter = [self getcharoccurencies:charusedtoformat instring:textstring]; int commadif = commmacountafter - commmacountbefore; int cursorpossition = (int)range.location + (int)string.length + commadif; //set cursor position nslog(@"cursorpossition: %d", cursorpossition); [self selecttextforinput:textfield atrange:nsmakerange(cursorpossition, 0)]; return no; }
Comments
Post a Comment