码迷,mamicode.com
首页 > 移动开发 > 详细

iOS随机改变背景色及显示数字

时间:2015-08-18 16:41:58      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:验证码   change   背景色   网站   

附上代码,类似于注册网站上的验证码

付图:

技术分享

技术分享

技术分享

- (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:1.0];

    [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++)

    {

        NSLog(@"rect.size.width / text.length * i%f",rect.size.width / 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:1.0];

        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);

    }

}


iOS随机改变背景色及显示数字

标签:验证码   change   背景色   网站   

原文地址:http://3767536.blog.51cto.com/3757536/1685541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!