Bank card formatted output and post four-bit display

Source: Internet
Author: User

#import <UIKit/UIKit.h>@interface  bankcardformat:uitextfield@end
#import "BankCardFormat.h"@interfaceBankcardformat () <UITextFieldDelegate>{nsstring*previoustextfieldcontent; Uitextrange*previousselection;}@end@implementationBankcardformat- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {self.Delegate=Self ; [Self addtarget:self Action: @selector (reformatascardnumber:) Forcontrolevents:uico    Ntroleventeditingchanged]; }        returnSelf ;}- (void) Reformatascardnumber: (Uitextfield *) textfield{Nsuinteger targetcursorposition=[TextField offsetFromPosition:textField.beginningOfDocument ToPosition:textField.selectedTextRange.start]; NSString*cardnumberwithoutspaces =[self RemoveNonDigits:textField.text andpreservecursorposition:&Targetcursorposition]; if([cardnumberwithoutspaces length] > +) {[TextField settext:previoustextfieldcontent]; Textfield.selectedtextrange=previousselection; return; } nsstring*cardnumberwithspaces =[Self insertspaceseveryfourdigitsintostring:cardnumberwithoutspaces Andpreservecursorposition:&Targetcursorposition]; Textfield.text=cardnumberwithspaces; Uitextposition*targetposition =[TextField Positionfromposition:[textfield beginningofdocument]offset:targetcursorposition]; [TextField Setselectedtextrange:[textfield textrangefromposition:targetposition toposition:targetposition];}-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string{previoustextfieldcontent=Textfield.text; Previousselection=Textfield.selectedtextrange; returnYES;}-(NSString *) removenondigits:(NSString *)stringAndpreservecursorposition: (Nsuinteger *) cursorposition{Nsuinteger originalcursorposition= *cursorposition; Nsmutablestring*digitsonlystring = [nsmutablestringNew];  for(Nsuinteger i =0; i < [stringLength]; i++) {Unichar Charactertoadd= [stringCHARACTERATINDEX:I]; if(IsDigit (Charactertoadd)) {NSString*stringtoadd =[nsstring stringwithcharacters:&charactertoadd Length:1];        [Digitsonlystring Appendstring:stringtoadd]; } Else {            if(I <originalcursorposition) {                (*cursorposition)--; }        }    }        returndigitsonlystring;}-(NSString *) insertspaceseveryfourdigitsintostring: (NSString *)stringAndpreservecursorposition:(Nsuinteger *) cursorposition{nsmutablestring*stringwithaddedspaces = [nsmutablestringNew]; Nsuinteger cursorpositioninspacelessstring= *cursorposition;  for(Nsuinteger i =0; i < [stringLength]; i++) {        if((i >0) && ((i%4) ==0) {[Stringwithaddedspaces appendString:@" "]; if(I <cursorpositioninspacelessstring) {                (*cursorposition) + +; }} Unichar Charactertoadd= [stringCHARACTERATINDEX:I]; NSString*stringtoadd =[NSString stringwithcharacters:&charactertoadd Length:1];    [Stringwithaddedspaces Appendstring:stringtoadd]; }        returnstringwithaddedspaces;}@end
//a four-bit space is required when entering a bank card number/*method of Use: Bankcardformat *bankcardnumber = [[Bankcardformat alloc]initwithframe:cgrectmake (60, 122, 230, 50)]; Bankcardnumber.borderstyle = Uitextborderstyleline; Bankcardnumber.placeholder = @ "Please enter your bank card number";  [Self.view Addsubview:bankcardnumber]; *///the bank card only shows the following four-bit practice/* Original card number NSString * Origincardno = [_weddata objectatindex:i][@ "Bankcardno"]; NSString *firstcardno = [Origincardno substringtoindex:origincardno.length-4];D log (@ "")//after four digits nsstring *lastfourC Ardno = [Origincardno substringfromindex:origincardno.length-4];D log (@ "%@", Lastfourcardno);D log ("%lu", unsigned Long) origincardno.length);D log ("%lu", (unsigned long) firstcardno.length); NSString *replacecardno = [Firstcardno stringbyreplacingoccurrencesofstring:firstcardno withString:@ "************** "];D log ("%lu ", (unsigned long) replacecardno.length); NSString *transform;if (replacecardno.length>firstcardno.length) {transform = [Replacecardno substringToIndex: Firstcardno.length];} DLog ("%lu", (unsigned long) transform.length);D log ("%@", Replacecardno); NSString *cardnumberstr = [NSString stringwithformat:@ "%@%@", Transform,lastfourcardno]; NSString *tmpstr = Cardnumberstr;dlog ("%@", Cardnumberstr);D log ("%@", tmpstr); int size = (int) TMPSTR.LENGTH/4; Nsmutablearray *tmpstrarr= [[Nsmutablearray alloc] init];for (int n = 0;n < size; n++) {[Tmpstrarr addobject:[tmpstr Substringwithrange:nsmak Erange (N*4, 4)];} [Tmpstrarr addobject:[tmpstr Substringwithrange:nsmakerange (size*4, (tmpstr.length% 4))]];tmpStr = [TMPSTRARR componentsjoinedbystring:@ ""];D log ("%lu", (unsigned long) tmpstr.length);D log ("%@", tmpstr);//  TMPSTR is the bank card number that finally shows only the last four digits.*/

Bank card formatted output and post four-bit display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.