@@ -8,6 +8,67 @@ class KeyboardViewController: UIInputViewController {
88
99 @IBOutlet var nextKeyboardButton : UIButton !
1010
11+ let keys : [ [ String ] ] = [
12+ [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " 0 " ] ,
13+ [ " q " , " w " , " e " , " r " , " t " , " y " , " u " , " i " , " o " , " p " ] ,
14+ [ " a " , " s " , " d " , " f " , " g " , " h " , " j " , " k " , " l " ] ,
15+ [ " z " , " x " , " c " , " v " , " b " , " n " , " m " ] ,
16+ [ " " ] ,
17+ ]
18+
19+ @objc private func keyPressed( _ sender: UIButton ) {
20+ guard let title = sender. currentTitle else { return }
21+ if !processKey( title) {
22+ textDocumentProxy. insertText ( title)
23+ }
24+ }
25+
26+ private func createButton( title: String ) -> UIButton {
27+ let button = UIButton ( type: . system)
28+ button. setTitle ( title, for: . normal)
29+ button. titleLabel? . font = UIFont . systemFont ( ofSize: 24 )
30+ button. backgroundColor = UIColor . gray. withAlphaComponent ( 0.2 )
31+ button. layer. cornerRadius = 8
32+ button. addTarget ( self , action: #selector( keyPressed ( _: ) ) , for: . touchUpInside)
33+ return button
34+ }
35+
36+ private func setupKeyboardLayout( ) {
37+ // Create a vertical stack view for rows
38+ let stackView = UIStackView ( )
39+ stackView. axis = . vertical
40+ stackView. distribution = . fillEqually
41+ stackView. alignment = . fill
42+ stackView. spacing = 5
43+
44+ // Create buttons for each row and add them to the stack view
45+ for row in keys {
46+ let rowStackView = UIStackView ( )
47+ rowStackView. axis = . horizontal
48+ rowStackView. distribution = . fillEqually
49+ rowStackView. alignment = . fill
50+ rowStackView. spacing = 5
51+
52+ for key in row {
53+ let button = createButton ( title: key)
54+ rowStackView. addArrangedSubview ( button)
55+ }
56+ stackView. addArrangedSubview ( rowStackView)
57+ }
58+
59+ // Add the stack view to the view controller's view
60+ stackView. translatesAutoresizingMaskIntoConstraints = false
61+ view. addSubview ( stackView)
62+
63+ // Set up Auto Layout constraints for the stack view
64+ NSLayoutConstraint . activate ( [
65+ stackView. leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: 10 ) ,
66+ stackView. trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: - 10 ) ,
67+ stackView. topAnchor. constraint ( equalTo: view. topAnchor, constant: 10 ) ,
68+ stackView. bottomAnchor. constraint ( equalTo: view. bottomAnchor, constant: - 10 ) ,
69+ ] )
70+ }
71+
1172 override func updateViewConstraints( ) {
1273 super. updateViewConstraints ( )
1374
@@ -19,6 +80,7 @@ class KeyboardViewController: UIInputViewController {
1980
2081 startFcitx ( Bundle . main. bundlePath)
2182 focusIn ( self . textDocumentProxy)
83+ setupKeyboardLayout ( )
2284
2385 // Perform custom UI setup here
2486 self . nextKeyboardButton = UIButton ( type: . system)
0 commit comments