SoFunction
Updated on 2025-04-13

iOS generated image numeric letter verification effect

This article shares the specific code for iOS generated image digital letter verification for your reference. The specific content is as follows

Directly upload the code, the comments are very detailed

#import ""

#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];

//#define kRandomColor [UIColor grayColor];
#define kLineCount 6
#define kLineWidth 1.0
#define kCharCount 4
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]

@implementation CaptchaView
@synthesize changeString,changeArray;

- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) {

     = 5.0; //Set the layer rounded corner radius     = YES; //Hide boundaries     = kRandomColor;

    //    [UIColor grayColor]

    //Show a random verification code    [self changeCaptcha];
  }

  return self;
}
#pragma mark Change verification code and get the string of the replaced verification code-(void)changeCaptcha
{
  //<1> Randomly extract the corresponding number of characters from the character array to form a verification code string  //The array stores all optional characters, either letters or Chinese   = [[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];

  //If you can determine the maximum required capacity, use initWithCapacity: to set it. The advantage is that when the number of elements does not exceed the capacity, adding elements does not require reallocating memory.  NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
   = [[NSMutableString alloc] initWithCapacity:kCharCount];

  // Randomly select the characters that require a number from the array, and then splice them into a string  for(int i = 0; i &lt; kCharCount; i++)
  {
    NSInteger index = arc4random() % ([ count] - 1);
    getStr = [ objectAtIndex:index];

     = (NSMutableString *)[ stringByAppendingString:getStr];
  }
}

#pragma mark Called when clicking on the view, because the current class itself is a UIView. Click to change the verification code to write it directly into this method without adding additional gestures-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  //Click on the interface to switch the verification code  [self changeCaptcha];

  //setNeedsDisplay calls the drawRect method to implement view drawing  [self setNeedsDisplay];
}

#pragma mark draw interface (automatically called after initialization; 2. It will be automatically called when calling the setNeedsDisplay method)- (void)drawRect:(CGRect)rect {
  // To override the parent class method, you must first call the parent class method  [super drawRect:rect];

  //Set random background colors   = kRandomColor;

  //Get the string to display the verification code, and calculate the approximate position of each character according to the length.  NSString *text = [NSString stringWithFormat:@"%@",];
  CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
  int width =  /  - ;
  int height =  - ;
  CGPoint point;

  //Draw each character in sequence, and you can set the font size, color, style, etc. of each character displayed.  float pX, pY;
  for (int i = 0; i &lt; ; i++)
  {
    pX = arc4random() % width +  /  * i;
    pY = arc4random() % height;
    point = CGPointMake(pX, pY);
    unichar c = [text characterAtIndex:i];
    NSString *textC = [NSString stringWithFormat:@"%C", c];

    [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}];
  }

    //Call drawRect: Before, the system will press a CGContextRef into the stack, and calling UIGraphicsGetCurrentContext() will take the CGContextRef at the top of the stack.    CGContextRef context = UIGraphicsGetCurrentContext();
    //Set the line drawing width    CGContextSetLineWidth(context, kLineWidth);

    //Draw the colored lines of interference    for(int i = 0; i &lt; kLineCount; i++)
    {
      //Set the random color of the line      UIColor *color = kRandomColor;
      CGContextSetStrokeColorWithColor(context, [color CGColor]);
      //Set the starting point of the line      pX = arc4random() % (int);
      pY = arc4random() % (int);
      CGContextMoveToPoint(context, pX, pY);
      //Set the end point of the line      pX = arc4random() % (int);
      pY = arc4random() % (int);
      CGContextAddLineToPoint(context, pX, pY);
      //Draw lines      CGContextStrokePath(context);
    }
}
@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.