自定義數字鍵盤-swift4.0
先不說話,先上效果圖。
用的是swift4.0語法編寫的頁面,確定的回撥還沒寫,用的時候,自己寫下即可。
以下是定義的View的程式碼:
//
// XCMoneyOfkeyboard.swift
// 自定義金額輸入鍵盤
//
// Created by 小崔 on 2018/11/5.
// Copyright © 2018年 All rights reserved.
//
import UIKit
private let KEYSCREENNW = UIScreen.main.bounds.size.width
private let KEYSCREENH = UIScreen.main.bounds.size.height
private let KeyBorodBtnW = (KEYSCREENNW-50)/4
private let KeyBorodBtnH = KeyBorodBtnW/2
private let KeyTopViewH = KeyBorodBtnH
private let KeyMidleViewH = KeyBorodBtnH*3
private let KeyBoradViewH = (KeyBorodBtnH*4) + 40 + 30
private let KeyContentViewH = KeyTopViewH + KeyMidleViewH + KeyBoradViewH
private let KEY_COLOR_BACKGROUND = UIColor(red: 237.0/255, green: 237.0/255, blue: 237.0/255, alpha: 1)
class XCMoneyOfkeyboard: UIView {
//顯示輸入金額
var showInputMoney:String?
//灰色的遮罩
private lazy var bgView:UIView = {
let aView = UIView()
aView.frame = CGRect(x: 0, y: 0, width: KEYSCREENNW, height: KEYSCREENH)
aView.alpha = 0.3
aView.backgroundColor = UIColor.black
return aView
}()
//內容的View
private lazy var contentView:UIView = {
let aview = UIView()
aview.backgroundColor = UIColor.white
return aview
}()
//頭部view
private lazy var topView : UIView = {
let aview = UIView()
aview.backgroundColor = KEY_COLOR_BACKGROUND
aview.isUserInteractionEnabled = true
return aview
}()
///取消按鈕
private lazy var cancelBtn:UIButton = {
let abtn = UIButton()
abtn.setTitle("取消", for: .normal)
abtn.setTitleColor(UIColor.gray, for: .normal)
abtn.layer.borderColor = UIColor.lightGray.cgColor
abtn.layer.borderWidth = 1.0
abtn.addTarget(self, action: #selector(XCMoneyOfkeyboard.cancelBtnClick), for: .touchUpInside)
return abtn
}()
//金額輸入顯示的View
private lazy var mideView:UIView = {
let aview = UIView()
aview.backgroundColor = UIColor.white
return aview
}()
//顯示金額¥符
private lazy var moneyF:UILabel = {
let alable = UILabel()
alable.text = "¥"
alable.textColor = UIColor.black
alable.font = UIFont.systemFont(ofSize: 30)
return alable
}()
///輸入金額的textFiled
private lazy var moneyTextF:UILabel = {
let atextF = UILabel()
atextF.textColor = UIColor.red
atextF.font = UIFont.systemFont(ofSize: 30)
atextF.text = "500,000.00"
return atextF
}()
//分割線
private lazy var line : UILabel = {
let alabel = UILabel()
alabel.backgroundColor = UIColor.lightText
return alabel
}()
///起購金額
private lazy var startTishi:UILabel = {
let alabel = UILabel()
alabel.text = "起購金額:"
alabel.textColor = UIColor.darkGray
alabel.font = UIFont.systemFont(ofSize: 15)
return alabel
}()
///起購金額
private lazy var startMoney:UILabel = {
let alabel = UILabel()
alabel.text = "¥2,000.00"
alabel.textColor = UIColor.darkGray
alabel.font = UIFont.systemFont(ofSize: 15)
return alabel
}()
///遞增金額
private lazy var increasTishi:UILabel = {
let alable = UILabel()
alable.text = "遞增金額:"
alable.font = UIFont.systemFont(ofSize: 15)
alable.textColor = UIColor.darkGray
return alable
}()
///遞增金額
private lazy var increasMMondy:UILabel = {
let alable = UILabel()
alable.text = "¥1000.00"
alable.font = UIFont.systemFont(ofSize: 15)
alable.textColor = UIColor.darkGray
return alable
}()
//鍵盤的View
private lazy var keyboradView:UIView = {
let aView = UIView()
aView.backgroundColor = KEY_COLOR_BACKGROUND
return aView
}()
override init(frame: CGRect) {
super.init(frame: frame)
showInputMoney = ""
configUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//畫頁面
extension XCMoneyOfkeyboard{
//1,初始化UI
func configUI(){
addSubview(bgView)
addSubview(contentView)
contentView.addSubview(topView)
topView.addSubview(cancelBtn)
contentView.addSubview(mideView)
mideView.addSubview(moneyF)
mideView.addSubview(moneyTextF)
mideView.addSubview(line)
mideView.addSubview(startMoney)
mideView.addSubview(startTishi)
mideView.addSubview(increasTishi)
mideView.addSubview(increasMMondy)
contentView.addSubview(keyboradView)
//內容View
contentView.frame = CGRect(x: 0, y: KEYSCREENH-KeyContentViewH, width: KEYSCREENNW, height: KeyContentViewH)
topView.frame = CGRect(x: 0, y: 0, width: KEYSCREENNW, height: KeyTopViewH)
cancelBtn.frame = CGRect(x: KEYSCREENNW-KeyBorodBtnW, y: 0, width: KeyBorodBtnW, height: KeyBorodBtnH)
mideView.frame = CGRect(x: 0, y: KeyTopViewH, width: KEYSCREENNW, height: KeyMidleViewH)
moneyF.frame = CGRect(x: 10, y: KeyBorodBtnH/2, width: 30, height: KeyBorodBtnH)
moneyTextF.frame = CGRect(x: 40, y: KeyBorodBtnH/2, width: KEYSCREENNW-80, height: KeyBorodBtnH)
line.frame = CGRect(x: 10, y: KeyBorodBtnH*2, width: KEYSCREENNW - 20, height: 1)
startTishi.frame = CGRect(x: 10, y: KeyBorodBtnH*2+1, width: KeyBorodBtnW, height: KeyBorodBtnH)
startMoney.frame = CGRect(x: 10+KeyBorodBtnW, y: KeyBorodBtnH*2+1, width: KeyBorodBtnW, height: KeyBorodBtnH)
increasTishi.frame = CGRect(x: 20 + KeyBorodBtnW*2, y: KeyBorodBtnH*2+1, width: KeyBorodBtnW, height: KeyBorodBtnH)
increasMMondy.frame = CGRect(x: 20 + KeyBorodBtnW*3, y: KeyBorodBtnH*2+1, width: KeyBorodBtnW, height: KeyBorodBtnH)
keyboradView.frame = CGRect(x: 0, y: KeyBorodBtnH*4, width: KEYSCREENNW, height: KeyBoradViewH)
drawKeyboard()
}
func drawKeyboard(){
var btnx:CGFloat = 10
var btny:CGFloat = 10
for i in 0...3{
btny = 10 + ((KeyBorodBtnH+10) * CGFloat(i))
for j in 0...3 {
btnx = 10 + ((KeyBorodBtnW+10) * CGFloat(j))
let btn = UIButton()
if i < 3 && j < 3 {//數字
btn.setTitle("\(j+(i*3+1))", for: .normal)
btn.tag = j+(i*3+1)
}
if i == 0 && j == 3 {//第一行第四列
btn.setTitle("刪除", for: .normal)
btn.tag = 101
}
if i == 1 && j == 3 {//第二行第四列
btn.setTitle("0000", for: .normal)
btn.tag = 102
}
if i == 3 {//第四行數字
if j == 0{
btn.setTitle("清除", for: .normal)
btn.tag = 103
}
if j == 1{
btn.setTitle("0", for: .normal)
btn.tag = 104
}
if j == 2{
btn.setTitle("00", for: .normal)
btn.tag = 105
}
}
if !(i == 3 && j == 3) {//第四行第四列
btn.setTitleColor(UIColor.black, for: .normal)
btn.backgroundColor = UIColor.white
btn.frame = CGRect(x: btnx, y: btny, width: KeyBorodBtnW, height: KeyBorodBtnH)
btn.setTitleColor(UIColor.red, for: .highlighted)
}
if i == 2 && j == 3 {//第三行第四列
btn.setTitleColor(UIColor.white, for: .normal)
btn.setTitle("確定", for: .normal)
btn.backgroundColor = UIColor.red
btn.tag = 106
btn.frame = CGRect(x: btnx, y: btny, width: KeyBorodBtnW, height: KeyBorodBtnH*2 + 10)
}
btn.addTarget(self, action: #selector(self.keyboardBtnClick(_:)), for: .touchUpInside)
keyboradView.addSubview(btn)
}
}
}
}
extension XCMoneyOfkeyboard{
@objc func cancelBtnClick(){
self.removeFromSuperview()
}
@objc func keyboardBtnClick(_ button:UIButton){
print("btnTag->\(button.tag)")
button.isHighlighted = true
if button.tag < 10{//點選數字
showInputMoney = showInputMoney! + "\(button.tag)"
}
if button.tag == 101{//點選刪除
if showInputMoney != ""{
let endIndex = showInputMoney?.index((showInputMoney?.endIndex)!, offsetBy: -1)
showInputMoney = "\(showInputMoney!.prefix(upTo: endIndex!))"
}
}
if button.tag == 102{//點選0000
showInputMoney = showInputMoney! + "0000"
}
if button.tag == 103{//點選清除
showInputMoney = ""
}
if button.tag == 104{//點選0
showInputMoney = showInputMoney! + "0"
}
if button.tag == 105{//點選00
showInputMoney = showInputMoney! + "00"
}
self.moneyTextF.text = showInputMoney
}
}