验证码(字母数字组合)

自定义 View

.h

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 }
span.s1 { color: #822e0e }
span.s2 { }
span.s3 { color: #c32275 }
span.s4 { color: #6122ae }
span.s5 { color: #000000 }

#import <UIKit/UIKit.h>

@interface PooCodeView : UIView

@property (nonatomic, retain) NSArray *changeArray;

@property (nonatomic, retain) NSMutableString *changeString;

@property (nonatomic, retain) UILabel *codeLabel;

-(void)changeCode;

@end

.m

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae }
span.s1 { color: #822e0e }
span.s2 { }
span.s3 { color: #000000 }
span.s4 { color: #c32275 }
span.s5 { color: #539aa4 }
span.s6 { color: #703daa }
span.s7 { color: #3d1d81 }
span.s8 { color: #0435ff }
span.s9 { color: #6122ae }
span.s10 { color: #294c50 }
span.s11 { color: #c91b13 }

#import "PooCodeView.h"

@implementation PooCodeView

@synthesize changeArray = _changeArray;

@synthesize changeString = _changeString;

@synthesize codeLabel = _codeLabel;

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// Initialization code

//        self.layer.cornerRadius = 5.0;

//        self.layer.masksToBounds = YES;

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)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

//{

//    [self change];

//    [self setNeedsDisplay];

//}

-(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" sizeWithFont:[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 withFont:[UIFont systemFontOfSize:20]];

}

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 1.0);

for(int cout = 0; cout < 10; 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

时间: 2024-10-15 02:52:23

验证码(字母数字组合)的相关文章

JS生成随机的由字母数字组合的字符串

前言 最近有个需求,是需要生成3-32位长度的字母数字组合的随机字符串,另一个是生成43位随机字符串. 方法一 奇妙的写法 1 Math.random().toString(36).substr(2); 输出结果 解释 很有意思,研究了一下,基本上toString后的参数规定可以是2-36之间的任意整数,不写的话默认是10(也就是十进制),此时返回的值就是那个随机数. 若是偶数,返回的数值字符串都是短的,若是奇数,则返回的将是一个很大长度的表示值. 若<10 则都是数字组成,>10 才会包含字

工具类:获得随机字母和数字的组合(字母+数字组合,字母组合,数字组合)

package util; import java.util.Random; /** * * @author jkfeng * 获得随机字母和数字的组合(字母+数字组合,字母组合,数字组合) * */ public class RandomCharOrNumUtil { public static void main(String[] args) { System.out.println(getCharAndNum(6)); System.out.println(getChar(6)); Sys

生成随机字母数字组合

//生成随机字母数字组合 public static string CreateRandom(int codeCount) { string allChar = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] allCharArray = allChar.Split(','); strin

字母数字组合 正则

http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp /^[0-9a-zA-Z]*$/g

PHP字母数字验证码和中文验证码

1:字母数字组合的验证码 HTML代码: 1 验证码:<input type="text" name="code"> 2 <img onclick="this.src='./gd.php?aa='+Math.random()" src="./gd.php"/> PHP代码: 1 //产生随机验证码字符 2 $str="abcdefghigklmnopqrstuvwxyz0123456789AB

js产生一个随机的字符串数字组合

/** * 随机生成字符串 * @param randomFlag 产生任意长度随机字母数字组合 * @param min 任意长度最小位[固定位数] * @param max 任意长度最大位 * @returns {string} */ function noncestr(randomFlag, min, max) { var str = "", range = min, arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

JAVAWEB项目实现验证码中文、英文、数字组合

验证码基础 一.什么是验证码及它的作用 :验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是必须只有人类才能解答.可以防止恶意破解密码.刷票.论坛灌水.有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录. 二.图文验证码的原理 :在servlet中随机生成一个指定位置的验证码,一般为四位,然后把该验证码保存到session中.在通过Java的绘图类以图片的形式输出该验证码.为了增加验证码的安全级别,可

字母汉子组合的验证码,包括实现看不清换一个的功能

1.写一个验证码,前台什么都不用写,这是CreateVerificationCode_Page.aspx.cs后台代码 using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlCo

获得N位数字字母随机组合

import string import random def get_rand(n): allw = string.letters+string.digits r = [] for i in range(n): r.append(random.choice(allw)) return ''.join(r) for i in range(4): print get_rand(8) 获得N位数字字母随机组合