验证码(字母数字组合)

自定义 View

.h

#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

#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

 

转载于:https://www.cnblogs.com/daxueshan/p/6093524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值