Generation of verification code

Related references

Basic principles

1. Store the characters, numbers and special characters to be displayed in an array.
2. Randomly get the specified number of characters from the array and drop them into the property string.
3. A certain range you set the x and y of each word in the property string.
4. Set the font size and left and right inclination of the attribute string to form the verification code.

Related code

#import <UIKit/UIKit.h>

@interface AuthcodeView : UIView

@property (strong, nonatomic) NSArray *dataArray;//Character material array

@property (strong, nonatomic) NSMutableString *authCodeStr;//Verification code string

@end



#import "AuthcodeView.h"

#define kCharCount 4 / / the number of words displayed in the verification code
#define kFontSize [UIFont systemFontOfSize:18] / / font size
@implementation AuthcodeView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.borderWidth = 1; // Border width
        self.layer.borderColor = [[UIColor darkGrayColor] CGColor];
        self.layer.cornerRadius = 2.0f;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self getAuthcode];//Obtain random verification code
    }
    return self;
}

#Obtain random verification code with pragma mark
- (void)getAuthcode
{
    //String material (only uppercase letters)
    _dataArray = [[NSArray alloc] initWithObjects:@"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];

    _authCodeStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
    //Randomly select the required number of strings from the array and splice them into a verification code string
    for (int i = 0; i < kCharCount; i++)
    {
        NSInteger index = arc4random() % (_dataArray.count-1);
        NSString *tempStr = [_dataArray objectAtIndex:index];
        _authCodeStr = (NSMutableString *)[_authCodeStr stringByAppendingString:tempStr];
    }
}

#pragma mark click interface switch verification code
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self getAuthcode];
    //setNeedsDisplay calls drawRect method to draw view
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    //Calculate the display position of each string according to the length of the verification code string to be displayed
    NSString *text = [NSString stringWithFormat:@"%@",_authCodeStr];

    CGSize cSize = [@"A" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];

    int height = rect.size.height - cSize.height;
    CGPoint point;

    //Draw each character in turn. You can set the font size, color, style, etc. of each character displayed
    float pX,pY;
    for ( int i = 0; i<text.length; i++)
    {
        pX = (rect.size.width-3)/text.length * i+3;
        pY = arc4random() % (height-5) + 2.5;
        point = CGPointMake(pX, pY);

        unichar c = [text characterAtIndex:i];
        NSString *textC = [NSString stringWithFormat:@"%C", c];
        // Nsobliqueessattributename set word skew angle, negative left, positive right
        [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize,NSObliquenessAttributeName:@(0.5-(arc4random()%100)/100.0)}];
    }

}


@end

Tags: iOS Attribute

Posted on Fri, 31 Jan 2020 06:05:53 -0800 by ramzess