[swift] 随机验证码

由于项目需要设计一个随机验证码显示器,固自己写了一个小demo,进行应用,特收集起来,以供使用

[swift] 随机验证码_第1张图片
CodeCaptcha.gif

代码如下

//
//  RandomCaptchaView.swift
//  RandomCaptchaView

import UIKit

class RandomCaptchaView: UIView {

    var changeString:String? //验证码的字符串
    
    private let kLineCount = 6
    private let kLineWidth = CGFloat(2.0)
    private let kCharCount = 4
    private let kFontSize = UIFont(name: "Georgia-BoldItalic", size: CGFloat(arc4random() % 5) + 25 )
    
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        let randomColor:UIColor = UIColor(red: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), green: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), blue: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), alpha: 0.5)
        
        
        
        
        self.layer.cornerRadius = 5.0   //设置layer圆角半径
        self.layer.masksToBounds = true //隐藏边界
        self.backgroundColor = randomColor
        
        self.getChangeCode()
    }
    
    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)!
        self.getChangeCode()
    }
    
    private func getChangeCode()
    {
        //字符素材数组
        let changeArray:NSArray = ["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"]
        
        self.changeString = ""
        //随机从数组中选取需要个数的字符,然后拼接为一个字符串
        
        for  _ in 0 ..< kCharCount {
            let index = Int(arc4random())%(changeArray.count - 1)
            let getStr = changeArray.object(at: index)
            self.changeString = self.changeString! + (getStr as! String)
        }
        print("验证码:\(changeString!)")
        
        
    }
    
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        getChangeCode()
        setNeedsDisplay()
        
    }
    
    
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        //背景颜色
        let randomBackColor = UIColor(red: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), green: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), blue: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), alpha: 1.0)
        self.backgroundColor = randomBackColor
        
        //获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置
        let str = NSString(string: "S")
        //        let font = UIFont.systemFontOfSize(20)
        let size = str.size(attributes: [NSFontAttributeName : kFontSize!])
        let width = rect.size.width / CGFloat(NSString(string: changeString!).length) - size.width
        let height = rect.size.height - size.height
        var point:CGPoint?
        var pX:CGFloat?
        var pY:CGFloat?
        
        //文字颜色
        let randomTextColor = UIColor(red: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), green: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), blue: CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX)), alpha: 1.0)
        
        for i in 0..

使用方法

[swift] 随机验证码_第2张图片
swift 随机验证码
let text1 = textField.text
        let text2 = randomCaptchaView.changeString
        //caseInsensitive 不区分大小写
        let result = text1?.range(of: text2!, options: .caseInsensitive)
        if result == nil {
            let alert = UIAlertView(title: nil, message: "验证码错误", delegate: self, cancelButtonTitle: "确定")
            alert.show()
        } else {
            let alert = UIAlertView(title: nil, message: "验证码正确", delegate: self, cancelButtonTitle: "确定")
            alert.show()
        }

你可能感兴趣的:([swift] 随机验证码)