美文网首页
iOS自定义控件-身份证键盘

iOS自定义控件-身份证键盘

作者: 今晚月色 | 来源:发表于2019-09-29 13:48 被阅读0次
镇楼专用

利用UIInputViewController自定义键盘,初次尝试。

实现代码

fileprivate class WDCustomKeybordButton: UIButton {
    
    // 按钮点击回调
    var didInputBlock: ((_ text: String) -> Void)?
    
    override var isHighlighted: Bool {
        didSet {
            // 监听是否是高亮状态,并设置背景颜色
            backgroundColor = isHighlighted ? UIColor(red:0.66, green:0.69, blue:0.73, alpha:1.00) : .white
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        layer.cornerRadius = 5
        setTitleColor(.black, for: .normal)
        addTarget(self, action: #selector(buttonTargetAction(sender:)), for: .touchUpInside)
    }
    
    @objc func buttonTargetAction(sender: UIButton) {
        let text = (sender.titleLabel?.text) ?? ""
        if didInputBlock != nil {
            didInputBlock!(text)
        }
        // 添加震动反馈
        UIImpactFeedbackGenerator().impactOccurred()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

@objc protocol WDCustomKeybordDelegate: NSObjectProtocol {
    func keybord(keybord: WDCustomKeybord, isDelete: Bool, text: String) -> Void
}

class WDCustomKeybord: UIInputViewController {
    
    /// 键盘点击Block回调,isDelete: 是否是删除按钮
    public var didInputBlock: ((_ isDelete: Bool, _ text: String) -> Void)?
    /// 代理回调
    public weak var delegate: WDCustomKeybordDelegate?
    /// 按钮的宽度
    private let buttonWidth: CGFloat = (UIScreen.main.bounds.width - 4.0 * 5.0) / 3.0
    /// 按钮高度
    private let buttonHeight: CGFloat = 40.0
    /// 间隙
    private let margin: CGFloat = 5.0
    /// 按钮数组
    private let numArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "X", "0", ""]
    /// 刘海屏适配
    private var isiPhoneX: Bool {
        var iPhoneX = false
        if UIDevice.current.userInterfaceIdiom != .phone {
            return iPhoneX
        }
        if #available(iOS 11.0, *) {
            let mainWindow = UIApplication.shared.keyWindow
            if (mainWindow?.safeAreaInsets.bottom)! > CGFloat(0.0) {
                iPhoneX = true
            }
        }
        return iPhoneX
    }
    
    override func viewDidLoad() {
        let height = buttonHeight * 4.0 + margin * 5.0
        if isiPhoneX {
            view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height + 34)
        } else {
            view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height)
        }
        setupSubviews()
    }
    
    /// for循环添加按钮
    private  func setupSubviews() {
        for (i, text) in numArray.enumerated() {
            let button = WDCustomKeybordButton()
            button.setTitle(text, for: .normal)
            button.tag = 1000 + I
            if i < 3 {
                button.frame = CGRect(x: margin + (margin + buttonWidth) * CGFloat(i), y: margin, width: buttonWidth, height: buttonHeight)
            } else if i < 6 {
                button.frame = CGRect(x: margin + (margin + buttonWidth) * CGFloat(i - 3), y: margin * 2 + buttonHeight, width: buttonWidth, height: buttonHeight)
            } else if i < 9 {
                button.frame = CGRect(x: margin + (margin + buttonWidth) * CGFloat(i - 6), y: margin * 3 + buttonHeight * 2, width: buttonWidth, height: buttonHeight)
            } else {
                button.frame = CGRect(x: margin + (margin + buttonWidth) * CGFloat(i - 9), y: margin * 4 + buttonHeight * 3, width: buttonWidth, height: buttonHeight)
            }
            
            if i == 11 {
                button.imageView?.contentMode = .scaleAspectFit
                // 删除按钮<自定义图片>
                button.setImage(UIImage(named: "delete"), for: .normal)
            }
            
            button.didInputBlock = {
                if self.didInputBlock != nil {
                    if $0.count == 0 {
                        self.didInputBlock!(true, $0)
                    } else {
                        self.didInputBlock!(false, $0)
                    }
                }
                
                if (self.delegate?.responds(to: #selector(WDCustomKeybordDelegate.keybord(keybord:isDelete:text:))))! {
                    if $0.count == 0 {
                        self.delegate?.keybord(keybord: self, isDelete:true , text: $0)
                    } else {
                        self.delegate?.keybord(keybord: self, isDelete:false , text: $0)
                    }
                }
            }
            view.addSubview(button)
        }
    }
}

使用方法(一)

         let inputController = WDCustomKeybord()
         inputController.didInputBlock = {
             if $0 { // 删除
                var varText = ""
                if let tText = self.textField.text {
                     for (i, text) in tText.enumerated() {
                         if i < tText.count - 1 {
                             varText.append(text)
                         }
                     }
                     self.textField.text = varText
                 }
             } else {// 输入
                 self.textField.text?.append($1)
             }
         }
         textField.inputView = inputController.inputView

使用方法(二)

        let inputController = WDCustomKeybord()
        inputController.delegate = self
        textField.inputView = inputController.inputView

// MARK: WDCustomKeybord Delegate Method
extension ViewController: WDCustomKeybordDelegate {
    func keybord(keybord: WDCustomKeybord, isDelete: Bool, text: String) {
        if isDelete { // 删除
            var varText = ""
            if let tText = textField.text {
                for (i, text) in tText.enumerated() {
                    if i < tText.count - 1 {
                        varText.append(text)
                    }
                }
                textField.text = varText
            }
        } else { // 输入
            textField.text?.append(text)
        }
    }
}

效果图

效果图.gif

相关文章

网友评论

      本文标题:iOS自定义控件-身份证键盘

      本文链接:https://www.haomeiwen.com/subject/alefpctx.html