import UIKit
class SecondVC: UIViewController,UITextFieldDelegate {
var tf1:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
let title = UILabel(frame: CGRect(x: 10, y: 130, width: Int(self.view.frame.width-20)/2, height: 50))
self.view.addSubview(title)
title.text = “请输入手机号”
title.font = UIFont.systemFont(ofSize: 30)
title.textColor = UIColor.black
let view1 = UIView(frame: CGRect(x: 10, y: Int(title.frame.height+130+20), width: Int(self.view.frame.width-20), height: 60))
view1.backgroundColor = UIColor.red
self.view.addSubview(view1)
let head = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
view1.addSubview(head)
head.setTitle("+86", for: UIControl.State.normal)
head.setTitleColor(UIColor.gray, for: .normal)
tf1 = UITextField(frame: CGRect(x: head.frame.width, y: 0, width: view1.frame.width-head.frame.width, height: 60))
view1.addSubview(tf1!)
tf1!.clearButtonMode = .always
tf1?.delegate = self
let btn = UIButton(frame: CGRect(x: 10, y: title.frame.height+130+20+view1.frame.height+10, width: self.view.frame.width-20, height: 60))
self.view.addSubview(btn)
btn.backgroundColor = UIColor.green
btn.setTitle(“下一步”, for: .normal)
btn.addTarget(self, action: #selector(jumpNext), for:.touchUpInside)
let btn1 = UIButton(frame: CGRect(x: 10, y: title.frame.height+130+20+view1.frame.height+10+btn.frame.height+5, width: 150, height: 40))
self.view.addSubview(btn1)
btn1.backgroundColor = UIColor.white
btn1.setTitle(“账户密码登录”, for: .normal)
btn1.setTitleColor(UIColor.gray,for:.normal)
btn1.addTarget(self, action: #selector(jumpBack), for:.touchUpInside)
}
@objc func jumpBack() {
let vc = ViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
@objc func jumpNext() {
if tf1?.text=="" {
return
}
if (tf1?.text?.IsLegalForPhoneNumber())! {
let vc = ThreeVC()
self.navigationController?.pushViewController(vc, animated: true)
}else{
let alertController = UIAlertController(title: “重新输入手机号”,message:nil,preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: “确定”,style: UIAlertAction.Style.cancel){(cancle: UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == self.tf1 {
if (textField.text?.count)!>=11{
return false
}
}
return true
}
}

import UIKit
class ThreeVC: UIViewController,UITextFieldDelegate {
var tf1:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
let title = UILabel(frame: CGRect(x: 10, y: 130, width: Int(self.view.frame.width-20), height: 50))
self.view.addSubview(title)
title.text = “输入短信验证码”
title.font = UIFont.systemFont(ofSize: 30)
title.textColor = UIColor.black
let view1 = UIView(frame: CGRect(x: 10, y: Int(title.frame.height+130+20), width: Int(self.view.frame.width-20), height: 60))
view1.backgroundColor = UIColor.red
self.view.addSubview(view1)
tf1 = UITextField(frame: CGRect(x: 10, y: 0, width: view1.frame.width, height: 60))
view1.addSubview(tf1!)
tf1!.clearButtonMode = .always
tf1?.delegate = self
let btn = UIButton(frame: CGRect(x: 10, y: title.frame.height+130+20+view1.frame.height+10, width: self.view.frame.width-20, height: 60))
self.view.addSubview(btn)
btn.backgroundColor = UIColor.green
btn.setTitle(“下一步”, for: .normal)
btn.addTarget(self, action: #selector(jumpNext), for:.touchUpInside)
let btn1 = UIButton(frame: CGRect(x: 10, y: title.frame.height+130+20+view1.frame.height+10+btn.frame.height+5, width: 150, height: 40))
self.view.addSubview(btn1)
btn1.backgroundColor = UIColor.white
btn1.setTitle(“使用语音验证码”, for: .normal)
btn1.setTitleColor(UIColor.gray,for:.normal)
btn1.addTarget(self, action: #selector(jumpBack), for:.touchUpInside)
}
@objc func jumpBack() {
}
@objc func jumpNext() {
if (tf1?.text?.IsLegalForPhoneYanZheng())! {
let alertController = UIAlertController(title: “验证码正确”,message:nil,preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: “确定”,style: UIAlertAction.Style.cancel){(cancle: UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}else{
let alertController = UIAlertController(title: “验证码不正确”,message:nil,preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: “确定”,style: UIAlertAction.Style.cancel){(cancle: UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == self.tf1 {
if (textField.text?.count)!>=6{
return false
}
}
return true
}
}
// MARK: - String类型的拓展
extension String{
/// 手机号正则
///
/// - Returns: true or false
func IsLegalForPhoneYanZheng()->Bool{
for char in self{
if char == “_”{
return false
}
}
var result = “”
// - 1、创建规则
let pattern1 = “[0-9]{4,6}”
// let pattern1 = “1[a-zA-Z0-9\u4e00-\u9fa5]$”
// - 2、创建正则表达式对象
let regex1 = try! NSRegularExpression(pattern: pattern1, options: NSRegularExpression.Options.caseInsensitive)
// - 3、开始匹配
let res = regex1.matches(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.count))
// 输出结果
for checkingRes in res {
result = result + (self as NSString).substring(with: checkingRes.range)
}
if result == self{
return true
}else{
return false
}
}
}
a-zA-Z\u4e00-\u9fa5 ↩︎
这篇博客展示了如何使用Swift创建一个登录页面,包括输入手机号、显示验证码和验证输入的环节。用户界面包含红色背景的输入框,绿色的下一步按钮,以及用于切换登录方式的白色按钮。代码中还实现了手机号格式的正则表达式校验。
1280

被折叠的 条评论
为什么被折叠?



