iOS开发----仿照qq登陆界面以及限制密码

由于我用到了storyboard,所以关于控件的的设置什么的可能不能在代码中展现,话不多说,直接贴上代码吧,相关的我都写在注释里面了

//
//  ViewController.swift
//  PasswordControl
//
//  Created by wyh on 2016/10/9.
//  Copyright © 2016年 wyh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var password: UITextField!
    
    @IBOutlet weak var btnLogin: UIButton!
    
    //正则表达式匹配正确的密码,没有特殊字符
    func checkPassword(str:String) -> Bool {
        do{
            let pattern = "[A-Za-z0-9]+";
            let regex:NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive);
            let matches = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, str.characters.count))
            return matches.count > 0;
        }
        catch{
            return false;
        }
    }
    //检查密码有没有相同字符
    func checkPasswordHasNoSameCharacter(str:String) -> Bool {
        //swift3 已经没有for(var i = 0 ; i < str.length;++i)这样的语法了
        var i = 0;
        for temp in str.characters{
            i += 1;
            let index = str.index(str.startIndex,offsetBy:i);
            let s = str.substring(from:index);
            if(s.contains(String(temp))){
                return false
            }
        }
        return true;
    }
    //检测字符串中不包含shift和dead
    func checkPasswordHasNoString(str:String,word_1:String,word_2:String) -> Bool {
        if((str.contains(word_1))||(str.contains(word_2))){
            return false;
        }
        return true;
    }
    @IBAction func unableToLogin(_ sender: UIButton) {
        
        //弹框对话控制器
        let alertController = UIAlertController(title: "对不起", message: "本功能未实现", preferredStyle: UIAlertControllerStyle.alert)
        //取消动作
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: {(UIAlertAction)->Void in
            print("取消操作");
        });
        //确认动作
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
        //添加动作
        alertController.addAction(cancelAction);
        alertController.addAction(okAction);
        //在self上弹出一个视图
        self.present(alertController, animated: true, completion:nil)
    }
    
    @IBAction func newComer(_ sender: UIButton) {
        let alertController = UIAlertController(title: "对不起", message:"本功能未实现", preferredStyle: UIAlertControllerStyle.alert);
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: {(UIAlertAction)->Void in
            print("取消操作");
        });
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
        alertController.addAction(cancelAction);
        alertController.addAction(okAction);
        self.present(alertController, animated: true, completion:nil);

    }
    
    @IBAction func btnLogin(_ sender: UIButton) {
        
        let userNameString = userName.text?.trimmingCharacters(in: NSCharacterSet.whitespaces);
        let passwordString = password.text?.trimmingCharacters(in: NSCharacterSet.whitespaces);
        var flag = true;
        //用户名密码不为空
        if((userNameString != "")&&(passwordString != ""))
        {
            if((passwordString?.characters.count)! > 8){
                flag = checkPassword(str: passwordString!);
                flag = checkPasswordHasNoSameCharacter(str: passwordString!);
                flag = checkPasswordHasNoString(str: passwordString!, word_1: "shift",word_2: "dead");
            }
            else{
                flag = false;
            }
        }
        else
        {
            flag = false;
        }
        if(flag)
        {
            //bundle表示路径,completion表示回调函数
            let sb = UIStoryboard(name: "Main", bundle: nil);
            let secondViewController = sb.instantiateViewController(withIdentifier: "second") as! SecondPageController;
            self.present(secondViewController, animated: true, completion: nil);
        }
        else
        {
            let alertController = UIAlertController(title: "对不起", message:"您的输入有误", preferredStyle: UIAlertControllerStyle.alert);
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
            alertController.addAction(okAction);
            self.present(alertController, animated: true, completion:nil)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        userName.adjustsFontSizeToFitWidth=true  //当文字超出文本框宽度时,自动调整文字大小
        userName.minimumFontSize=14  //最小可缩小的字号
        
        //设置view的背景图片
        self.view.backgroundColor = UIColor(patternImage: UIImage(named:"login.jpg")!)
        
        //圆角按钮
        btnLogin.layer.cornerRadius = 4.0;
        btnLogin.layer.masksToBounds = true;
        //圆形头像
        let headImageView = UIImageView();
        let headImage = UIImage(named:"wyh.jpg");
        let headImageSize:CGFloat = 75.0;
        
        //frame设置其位置和大小,而bounds只能设置其大小,其参数中的x、y不起作用即便是之前没有设定frame属性,控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。
        headImageView.bounds = CGRect(x:(self.view.bounds.size.width - headImageSize)/2,y:(self.view.bounds.size.width - headImageSize)/2-150,width:headImageSize,height:headImageSize);
        
        headImageView.frame = CGRect(x:(self.view.bounds.size.width - headImageSize)/2,y:(self.view.bounds.size.width - headImageSize)/2-75,width:headImageSize,height:headImageSize)
        
        //设置圆形的半径
        headImageView.layer.cornerRadius = headImageView.bounds.height/2;
        //设置遮罩,true代表显示在最上面,iOS中视图可以理解为一层一层的图层
        headImageView.layer.masksToBounds = true;
        //圆形边框的属性
        headImageView.layer.borderColor = UIColor.gray.cgColor;
        headImageView.layer.borderWidth = 3;
        
        headImageView.image = headImage;
        self.view.addSubview(headImageView);
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
第二个界面

//
//  SecondPageController.swift
//  PasswordControl
//
//  Created by wyh on 2016/10/9.
//  Copyright © 2016年 wyh. All rights reserved.
//

import UIKit

class SecondPageController: UIViewController {

    
    @IBOutlet weak var btnReturnToFirstPage: UIButton!
    @IBOutlet weak var btnConvert: UIButton!
    
    @IBOutlet weak var convertText: UITextField!
    
    @IBAction func returnToFirstPage(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    @IBAction func convertText(_ sender: UIButton) {
        let str = convertText.text?.trimmingCharacters(in: NSCharacterSet.whitespaces);
        var result = "";
        for temp in (str?.characters)!{
            if(temp == ",")
            {
                let index = result.index(result.endIndex,offsetBy:-1);
                let s = result.substring(from:index).uppercased();
                let formerStr = result.substring(to:index);
                result = formerStr + s + " ";
            }
            else if(temp == ";")
            {
                let index = result.index(result.endIndex,offsetBy:-1);
                let s = result.substring(from:index).lowercased();
                let formerStr = result.substring(to:index);
                result = formerStr + s + " ";
            }
            else
            {
                result.append(temp);
            }
        }
        convertText.text = result;
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        btnReturnToFirstPage.layer.cornerRadius = 4.0;
        btnReturnToFirstPage.layer.masksToBounds = true;
        btnConvert.layer.cornerRadius = 4.0;
        btnConvert.layer.masksToBounds = true;
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
下面附上我工程的源文件

点击打开链接





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值