import UIKit
class CaptchaView: UIView {
private let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
private let numberOfCharacters = 6
private let fontSize: CGFloat = 20.0
private var captchaText: String = ""
override func awakeFromNib() {
super.awakeFromNib()
generateCaptcha()
backgroundColor = UIColor.lightGray
}
// 生成随机的验证码
private func generateCaptcha() {
captchaText = ""
for _ in 0..<numberOfCharacters {
let randomIndex = Int(arc4random_uniform(UInt32(characters.count)))
let randomCharacter = characters[characters.index(characters.startIndex, offsetBy: randomIndex)]
captchaText.append(randomCharacter)
}
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
let width = rect.size.width / CGFloat(numberOfCharacters)
for (index, character) in captchaText.enumerated() {
let x = CGFloat(index) * width
let y = rect.size.height / 2 - fontSize / 2
let font = UIFont.systemFont(ofSize: fontSize)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: UIColor.black
]
let attributedString = NSAttributedString(string: String(character), attributes: attributes)
let characterRect = CGRect(x: x, y: y, width: width, height: fontSize)
attributedString.draw(in: characterRect)
}
}
}
在上述代码中,CaptchaView是一个继承自UIView的自定义视图,用于展示图形验证码。在awakeFromNib()方法中,我们调用generateCaptcha()生成随机的验证码并设置背景色。在draw(_:)方法中,我们使用Core Graphics绘制验证码的每个字符,并将其居中显示在视图中。你可以将CaptchaView添加到你的iOS应用程序的界面中,然后就可以看到生成的图形验证码了。