gpt4 book ai didi

ios - 如何使 UIKeyboardType 仅用于十六进制输入?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:26 26 4
gpt4 key购买 nike

swift 中是否有任何选项可以使用扩展名或任何其他方式制作仅具有 HEX(0-9,A,B,C,D,E,F) 值的新 UIKeyboardType?我想要只启用十六进制字符的键盘,用户可以清楚地看到他只能输入十六进制字符,或者键盘上只能看到十六进制字符

最佳答案

按照 Saurabh Prajapati 的建议,以下代码创建一个十六进制键盘并将其传递给 inputView。

键盘是这样设计的:

enter image description here

设计基于 David Mulder 的布局 https://ux.stackexchange.com/a/58605/128044

HexadecimalKeyboard 类创建键盘。

protocol RemoveKeyboardDelegate: class {
func removeKeyboard()
}

class HexButton: UIButton {
var hexCharacter: String = ""
}

class HexadecimalKeyboard: UIView {
weak var target : UIKeyInput?
weak var delegate : RemoveKeyboardDelegate?

var hexadecimalButtons: [HexButton] = ["0","7","8","9","4","5","6","1","2","3","A","B","C","D","E","F"].map {
let button = HexButton(type: .system)
button.hexCharacter = $0
button.setTitle("\($0)", for: .normal)
button.backgroundColor = UIColor.secondarySystemGroupedBackground
button.addTarget(self, action: #selector(didTapHexButton(_:)), for: .touchUpInside)
return button
}

var deleteButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("⌫", for: .normal)
button.backgroundColor = UIColor.systemGray4
button.accessibilityLabel = "Delete"
button.addTarget(self, action: #selector(didTapDeleteButton(_:)), for: .touchUpInside)
return button
}()

var okButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("OK", for: .normal)
button.backgroundColor = UIColor.systemGray4
button.accessibilityLabel = "OK"
button.addTarget(self, action: #selector(didTapOKButton(_:)), for: .touchUpInside)
return button
}()

var mainStack: UIStackView = {
let stackView = UIStackView()
stackView.distribution = .fillEqually
stackView.spacing = 10
stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
stackView.isLayoutMarginsRelativeArrangement = true
stackView.layoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
return stackView
}()

init(target: UIKeyInput) {
self.target = target
super.init(frame: .zero)
configure()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


// MARK: - Actions

extension HexadecimalKeyboard {
@objc func didTapHexButton(_ sender: HexButton) {
target?.insertText("\(sender.hexCharacter)")
}

@objc func didTapDeleteButton(_ sender: HexButton) {
target?.deleteBackward()
}

@objc func didTapOKButton(_ sender: HexButton) {
delegate?.removeKeyboard()
}
}


// MARK: - Private initial configuration methods

private extension HexadecimalKeyboard {
func configure() {
self.backgroundColor = .systemGroupedBackground
autoresizingMask = [.flexibleWidth, .flexibleHeight]
buildKeyboard()
}

func buildKeyboard() {
//MARK: - Add main stackview to keyboard
mainStack.frame = bounds
addSubview(mainStack)

//MARK: - Create stackviews
let panel1 = createStackView(axis: .vertical)
let panel2 = createStackView(axis: .vertical)
let panel2Group = createStackView(axis: .vertical)
let panel2Controls = createStackView(axis: .horizontal, distribution : .fillProportionally)


//MARK: - Create multiple stackviews for numbers
for row in 0 ..< 3 {
let panel1Numbers = createStackView(axis: .horizontal)
panel1.addArrangedSubview(panel1Numbers)

for column in 0 ..< 3 {
panel1Numbers.addArrangedSubview(hexadecimalButtons[row * 3 + column + 1])
}
}

//MARK: - Create multiple stackviews for letters
for row in 0 ..< 2 {
let panel2Letters = createStackView(axis: .horizontal)
panel2Group.addArrangedSubview(panel2Letters)

for column in 0 ..< 3 {
panel2Letters.addArrangedSubview(hexadecimalButtons[9 + row * 3 + column + 1])
}
}

//MARK: - Nest stackviews
mainStack.addArrangedSubview(panel1)
panel1.addArrangedSubview(hexadecimalButtons[0])
mainStack.addArrangedSubview(panel2)
panel2.addArrangedSubview(panel2Group)
panel2.addArrangedSubview(panel2Controls)
panel2Controls.addArrangedSubview(deleteButton)
panel2Controls.addArrangedSubview(okButton)

//MARK: - Constraint - sets okButton width to two times the width of the deleteButton plus 10 points for the space
panel2Controls.addConstraint(NSLayoutConstraint(
item : okButton,
attribute : .width,
relatedBy : .equal,
toItem : deleteButton,
attribute : .width,
multiplier : 2,
constant : 10))
}

func createStackView(axis: NSLayoutConstraint.Axis, distribution: UIStackView.Distribution = .fillEqually) -> UIStackView {
let stackView = UIStackView()
stackView.axis = axis
stackView.distribution = distribution
stackView.spacing = 10
return stackView
}
}

代码源自 Rob 提供的十进制键盘示例 https://stackoverflow.com/a/57275689/1816667

下面是一个如何使用键盘的例子。在示例中,两个文本字段设置为十六进制键盘:

class ViewController: UIViewController {

@IBOutlet var hexField: [UITextField]!

override func viewDidLoad() {
hexField[0].inputView = HexadecimalKeyboard(target: hexField[0])
hexField[1].inputView = HexadecimalKeyboard(target: hexField[1])
}

@IBAction func clickTextField(_ sender: UITextField) {
sender.reloadInputViews()
sender.inputView = HexadecimalKeyboard(target: sender)

let hexadecimalKeyboard = HexadecimalKeyboard(target: sender)
sender.inputView = hexadecimalKeyboard
hexadecimalKeyboard.delegate = self
}
} // end of View Controller

extension ViewController: RemoveKeyboardDelegate {
func removeKeyboard() {
_ = hexField.map { $0.inputView?.removeFromSuperview() }
}
}

此处提供了一个使用 Swift 5 的示例 Xcode 12 项目: https://github.com/PepperoniJoe/HexadecimalKeyboard

enter image description here

关于ios - 如何使 UIKeyboardType 仅用于十六进制输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46579414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com