. h Files
. m file
@synthesize Changearray = _changearray;
@synthesize changestring = _changestring;
@synthesize Codelabel = _codelabel;
-(ID) initWithFrame: (CGRect) frame {self = [super Initwithframe:frame];
if (self) {//initialization code FLOAT red = arc4random ()% 100/100.0;
float green = arc4random ()% 100/100.0;
float blue = arc4random ()% 100/100.0;
Uicolor *color = [Uicolor colorwithred:red green:green blue:blue alpha:0.2];
Self.backgroundcolor = color;
[Self change];
return self;
}-(void) ChangeCode {[Self change];
[Self setneedsdisplay]; }-(void) Change {self.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 ", @" 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]; Nsmutablestring *GETSTR = [[Nsmutablestring alloc] initwithcapacity:5];
self.changestring = [[Nsmutablestring alloc] initwithcapacity:6];
for (Nsinteger i = 0; i < 4; i++) {Nsinteger index = arc4random ()% ([Self.changearray count]-1);
GETSTR = [Self.changearray objectatindex:index];
self.changestring = (nsmutablestring *) [self.changestring stringbyappendingstring:getstr];
}}-(void) DrawRect: (cgrect) rect {[Super Drawrect:rect];
float red = arc4random ()% 100/100.0;
float green = arc4random ()% 100/100.0;
float blue = arc4random ()% 100/100.0;
Uicolor *color = [Uicolor colorwithred:red green:green blue:blue alpha:0.5];
[Self setbackgroundcolor:color];
NSString *text = [NSString stringwithformat:@ "%@", self.changestring];
Cgsize csize = [@ "S" Sizewithattributes:@{nsfontattributename:[uifont systemfontofsize:20]}];
int width = rect.size.width/text.length-csize.width;
int height = rect.size.height-csize.height;
Cgpoint Point; Float PX,PY;
for (int i = 0; i < text.length i++) {PX = Arc4random ()% width + rect.size.width/text.length * i;
PY = arc4random ()% height;
Point = Cgpointmake (PX, PY);
Unichar c = [text Characteratindex:i];
NSString *TEXTC = [NSString stringwithformat:@ "%c", C];
[TEXTC drawatpoint:point Withattributes:@{nsfontattributename:[uifont systemfontofsize:20]}];
} cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextsetlinewidth (context, 1.0);
for (int cout = 0; cout < cout++) {red = Arc4random ()% 100/100.0;
Green = arc4random ()% 100/100.0;
Blue = arc4random ()% 100/100.0;
color = [Uicolor colorwithred:red green:green blue:blue alpha:0.2];
Cgcontextsetstrokecolorwithcolor (context, [color Cgcolor]);
PX = Arc4random ()% (int) rect.size.width;
PY = Arc4random ()% (int) rect.size.height;
Cgcontextmovetopoint (context, PX, PY);
PX = Arc4random ()% (int) rect.size.width; PY = Arc4random ()% (int) Rect.sIze.height;
Cgcontextaddlinetopoint (context, PX, PY);
Cgcontextstrokepath (context); }} @end
The above is the use of iOS to draw pictures random verification code of the entire content, I hope this article on the content of iOS developers can help, if you have questions you can message exchange.