Record a sum:
Draw a local verification code view of a demo Source: (Source: http://www.cnblogs.com/jerehedu/p/4527707.html)
Code for the. h file:
1 #import <UIKit/UIKit.h>23@interface authcodeview:uiview4 5 @property (nonatomic, retain) nsmutablestring *changestring; // the string of the verification code 6 @end
Code for the. m file:
1 #import "AuthCodeView.h"2 3 #defineKrandomcolor [Uicolor colorwithred:arc4random ()% 256/255.0 green:arc4random ()% 256/255.0 blue:arc4random ()% 256/ 255.0 alpha:1.0];4 #defineKlinecount 65 #defineKlinewidth 1.06 #defineKcharcount 47 #definekfontsize [Uifont systemfontofsize:arc4random ()% 5 + 18]8 9 @implementationAuthcodeviewTen @synthesizechangestring; One A-(Instancetype) initWithFrame: (CGRect) Frame - { - if(self =[Super Initwithframe:frame]) { the -Self.layer.cornerRadius =5.0;//set the layer fillet radius -Self.layer.masksToBounds = YES;//Hide Boundary -Self.backgroundcolor =Krandomcolor; + - //show a random verification code + [self refreshcode]; A } at - returnSelf ; - } - #pragmaMark replaced the verification code to get the replacement captcha string --(void) Refreshcode { - //randomly extracts the corresponding number of characters from a character array, constituting a CAPTCHA string inNsarray *changearray = [[Nsarray alloc] Initwithobjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"a",@"b",@"C",@"D",@"e",@"F",@"g",@"h",@"I",@"J",@"k",@"L",@"m",@"N",@"o",@"P",@"Q",@"R",@"s",@"T",@"u",@"v",@"W",@"x",@"y",@"Z", nil]; - to //If you can determine the maximum required capacity, use initwithcapacity: To set, the advantage is that when the number of elements does not exceed the capacity, adding elements do not need to reallocate memory +Nsmutablestring *getstr =[[Nsmutablestring alloc] initwithcapacity:kcharcount]; -Self.changestring =[[Nsmutablestring alloc] initwithcapacity:kcharcount]; the * //randomly select the desired number of characters from the array and stitch it into a string $ for(inti =0; i < Kcharcount; i++)Panax Notoginseng { -Nsinteger index = arc4random ()% ([Changearray count]-1); theGetstr =[Changearray Objectatindex:index]; + Aself.changestring = (nsmutablestring *) [self.changestring stringbyappendingstring:getstr]; the } + } - $ #pragmaMark clicks on the view, because the current class itself is UIView, click the replacement verification code can be directly written into this method, no additional gestures added $-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { - //Click the interface, switch the verification code the [self refreshcode]; - Wuyi //Setneedsdisplay Call the DrawRect method to implement view rendering the [self setneedsdisplay]; - } Wu - #pragmaMark Draw Interface (1. UIView is automatically called after initialization; 2. Call the Setneedsdisplay method automatically) About- (void) DrawRect: (cgrect) rect { $ //overriding the parent class method, first calling the method of the parent class - [Super Drawrect:rect]; - - //set a random background color ASelf.backgroundcolor =Krandomcolor; + the //Get a verification code string to display, depending on the length, calculate the approximate position of each character display -NSString *text = [NSString stringWithFormat:@"%@", self.changestring]; $Cgsize cSize = [@"S"Sizewithattributes:@{nsfontattributename:[uifont systemfontofsize: -]}]; the intwidth = rect.size.width/text.length-Csize.width; the intHeight = rect.size.height-Csize.height; the Cgpoint Point; the - //draw each character in turn to set the font size, color, style, and so on for each character displayed in floatPX, PY; the for(inti =0; i < text.length; i++) the { AboutPX = arc4random ()% width + rect.size.width/text.length *i; thePY = arc4random ()%height; thePoint =Cgpointmake (PX, PY); theUnichar C =[text characteratindex:i]; +NSString *TEXTC = [NSString stringWithFormat:@"%c", c]; - the [TEXTC Drawatpoint:point withattributes:@{nsfontattributename:kfontsize}];Bayi } the the //call DrawRect: Before the system pushes a cgcontextref into the stack, the call Uigraphicsgetcurrentcontext () takes the top of the stack cgcontextref -Cgcontextref context =Uigraphicsgetcurrentcontext (); - //Set drawing line width the cgcontextsetlinewidth (context, klinewidth); the the //draw a colored line of interference the for(inti =0; i < Klinecount; i++) - { the //set the random color of a line theUicolor *color =Krandomcolor; the Cgcontextsetstrokecolorwithcolor (context, [color Cgcolor]);94 //set the starting point of a line thePX = Arc4random ()% (int) Rect.size.width; thePY = Arc4random ()% (int) Rect.size.height; the cgcontextmovetopoint (context, PX, PY);98 //set line end point AboutPX = Arc4random ()% (int) Rect.size.width; -PY = Arc4random ()% (int) Rect.size.height;101 cgcontextaddlinetopoint (context, PX, PY);102 //Draw Line103 Cgcontextstrokepath (context);104 } the }106 107 @end
How to use:
1 // Display Verification Code interface 2 Authcodeview *codeview = [[Authcodeview alloc] Initwithframe:cgrectmake (150 )]; 3 [Self.view Addsubview:codeview];
"IOS" uses UIView to draw local codes