仿微信--提现密码设置页 swift

《仿微信--提现密码设置页 swift》

几个关键点:

1.为什么用UITextField.leftView

避免了长按会出现放大镜(虽然可以重写UITextField的方法,禁止复制、粘贴、选择的功能,但是不能避免移动光标的位置。)如果光标的位置移动,则输入的位置可能会发生变化,添加删除的黑点就会错乱。

2.为什么进入页面直接显示键盘?

在 viewWillAppear 的时候`self.textField.becomeFirstResponder(),因为点击leftView上是不会弹出键盘的。

3.如何添加和删除黑点?

这里需要定义一个全局的数组,添加和删除UIView。

代码如下:

    func showViewOne() {
        
        viewOne = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        viewOne.backgroundColor = bgColor
        self.view.addSubview(viewOne)
        
        
        let tipLabel = UILabel.init(frame: CGRect.init(x: 20, y: SCREEN_HEIGHT/3-50, width: SCREEN_WIDTH-40, height: 40))
        tipLabel.text = "设置提现密码"
        tipLabel.textColor = UIColor.black
        tipLabel.textAlignment = NSTextAlignment.center
        tipLabel.font = UIFont.init(name: FONT, size: 16)
        viewOne.addSubview(tipLabel)
        
        passworldTF = UITextField.init(frame: CGRect.init(x: 20, y: tipLabel.bottom()+10, width: SCREEN_WIDTH-40, height: 40))
        if (SCREEN_WIDTH > 320) {
            passworldTF.frame = CGRect.init(x: 30, y: tipLabel.bottom()+10, width: SCREEN_WIDTH-60, height: 50)
        }
        passworldTF.backgroundColor = UIColor.white
        passworldTF.layer.borderWidth = 1
        passworldTF.layer.borderColor = lineColor.cgColor
        passworldTF.keyboardType = UIKeyboardType.numberPad
        passworldTF.tintColor = UIColor.clear
        passworldTF.textColor = UIColor.clear
        passworldTF.delegate = self
        passworldTF.leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: passworldTF.width(), height: passworldTF.height()))
        passworldTF.leftViewMode = UITextFieldViewMode.always
        viewOne.addSubview(passworldTF)
        
        let lineSpaceWidth = passworldTF.width() / 6
        
        //5条竖线
        var i = 1
        for _ in 1...5 {
            let xLine = CGFloat(i)*lineSpaceWidth
            let lineView = UIView.init(frame: CGRect.init(x: xLine, y: 0, width: 0.5, height: passworldTF.height()))
            lineView.backgroundColor = lineColor
            passworldTF.addSubview(lineView)
            i = i + 1
        }
        
    }
    
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if (textField == self.passworldTF) {
            if (range.location >= 6) {
                return false
            } else {
                
                if (range.length == 0) { //文本输入状态
                    let pointX = ((passworldTF.width()-62.5) / 12) * CGFloat(range.location * 2 + 1) + CGFloat(range.location) * 10.5
                    let pointY = (passworldTF.height()-10) / 2
                    //显示黑点
                    let blackPoint = UIView.init(frame: CGRect.init(x: pointX, y: pointY, width: 10.0, height: 10.0))
                    blackPoint.layer.cornerRadius = 5
                    blackPoint.backgroundColor = UIColor.black
                    passworldTF.addSubview(blackPoint)
                    pointViewArr.append(blackPoint)
                } else {  //文本删除状态
                    //移除黑点
                    pointViewArr[range.location].removeFromSuperview()
                    pointViewArr.remove(at: range.location)
                }
                
                if string == "" {
                    let word = self.password.substring(with: NSMakeRange(0, range.location))
                    self.password = word as NSString
                } else {
                    self.password = self.password.appending(string) as NSString
                }
                
                print(self.password)
                return true
            }
        }
        return true
    }
    原文作者:ZhugeZhan
    原文地址: https://segmentfault.com/a/1190000009428968
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞