如何使用应用内自定义键盘的按钮输入文本 [英] How to input text using the buttons of an in-app custom keyboard
问题描述
当我点击 UITextField
时,我制作了一个替换系统键盘并弹出的应用内自定义键盘。
这是我的代码:
class ViewController:UIViewController {
var myCustomKeyboard:UIView!
@IBOutlet weak var textField:UITextField!
覆盖func viewDidLoad(){
super.viewDidLoad()
let keyboardNib = UINib(nibName:Keyboard,bundle:nil)
myCustomKeyboard = keyboardNib.instantiateWithOwner(self,options:nil)[0] as! UIView
textField.inputView = myCustomKeyboard
}
}
键盘布局是从xib文件加载的。
问题
怎么做我将按钮文本输入文本字段?
注意:
- 有许多关于制作自定义系统键盘的教程(需要安装),但我只想要一个应用内键盘。教程使用一个特殊的视图控制器,仅用于键盘,但似乎我只是设置键盘视图。
- 我已经阅读了
-
Hoo将
swift
文件中的所有关键按钮添加到IBAction方法中。 (参见下面的代码。)
代码
我正在使用委托模式在自定义键盘视图和主视图控制器之间进行通信。这允许它们分离。可以交换多个不同的自定义键盘,而无需更改主视图控制器中的详细实现代码。
键盘。 swift
file
import UIKit
protocol KeyboardDelegate {
func keyWasTapped(character:String)
}
class键盘:UIView {
var delegate:KeyboardDelegate?
必需init?(编码器aDecoder:NSCoder){
super.init(编码器:aDecoder)
initializeSubviews()
}
override init(frame:CGRect){
super.init(frame:frame)
initializeSubviews()
}
func initializeSubviews(){
let xibFileName =Keyboard//不需要xib扩展
让view = NSBundle.mainBundle()。loadNibNamed(xibFileName,owner:self,options:nil)[0] as! UIView
self.addSubview(view)
view.frame = self.bounds
}
@IBAction func keyTapped(sender:UIButton){
self .delegate?.keyWasTapped(sender.titleLabel!。text!)
}
}
主视图控制器
请注意 ViewController
符合到我们创建的 KeyboardDelegate
协议。此外,在创建键盘视图的实例时,需要设置 height
,但 width
不需要。显然设置文本字段的 inputView
会将键盘视图宽度更新为屏幕宽度,这很方便。
class ViewController:UIViewController,KeyboardDelegate {
@IBOutlet weak var textField:UITextField!
覆盖func viewDidLoad(){
super.viewDidLoad()
//获取键盘实例(只有高度很重要)
让keyboardView = Keyboard(frame:CGRect(x:0,y:0,width:0,height:300))
//使用委托来传达
keyboardView.delegate = self
//用自定义键盘替换系统键盘
textField.inputView = keyboardView
}
//键盘委托协议所需的方法
func keyWasTapped(character:String){
textField.insertText(character)
}
}
来源
- @ ryancrunchi的评论很有帮助。
- 来自的答案使用xib创建可重用的UIView(并从故事板加载)
相关
I've made an in-app custom keyboard that replaces the system keyboard and pops up when I tap inside a UITextField
.
Here is my code:
class ViewController: UIViewController {
var myCustomKeyboard: UIView!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let keyboardNib = UINib(nibName: "Keyboard", bundle: nil)
myCustomKeyboard = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView
textField.inputView = myCustomKeyboard
}
}
The keyboard layout is loaded from an xib file.
Question
How do I get the button text into the text field?
Notes:
- There are many tutorials about making a custom system keyboard (which needs to be installed), but I only want an in-app keyboard. The tutorials use a special view controller just for the keyboard, but here it seems that I am just setting the keyboard view.
- I have read the Custom Views for Data Input documentation.
- This is the closest Stack Overflow question I could find, but it doesn't go as far as describing how to get the text from the buttons.
Update
- This tutorial seems to indicate that there is a view controller for the custom input view. However, I am getting lost in the Objective-C code. What would be the process in Swift?
- This answer mentions the
UIKeyInput
protocol thatUITextField
conforms to, but how do I use it? - If there is any built in way too make a custom in-app keyboard, I would really prefer that to making a normal custom view.
Setup
- Make an
xib
file that includes all your keys - Use Autolayout so that the keys will resize to the correct proportions no matter how big the keyboard is set to later.
Create a
swift
file with the same name as thexib
file and set it as the file owner in thexib
file setting.Hook up all the key buttons to an IBAction method in the
swift
file. (See the code below.)
Code
I'm using the delegate pattern to communicate between the custom keyboard view and the main view controller. This allows them to be decoupled. Multiple different custom keyboards could be swapped in and out without needing to change the detailed implementation code in the main view controller.
Keyboard.swift
file
import UIKit
protocol KeyboardDelegate {
func keyWasTapped(character: String)
}
class Keyboard: UIView {
var delegate: KeyboardDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeSubviews()
}
func initializeSubviews() {
let xibFileName = "Keyboard" // xib extention not needed
let view = NSBundle.mainBundle().loadNibNamed(xibFileName, owner: self, options: nil)[0] as! UIView
self.addSubview(view)
view.frame = self.bounds
}
@IBAction func keyTapped(sender: UIButton) {
self.delegate?.keyWasTapped(sender.titleLabel!.text!)
}
}
Main view controller
Note that the ViewController
conforms to the KeyboardDelegate
protocol that we created. Also, when creating an instance of the keyboard view, the height
needs to be set but the width
doesn't. Apparently setting the inputView
of the text field updates the keyboard view width to the screen width, which is convenient.
class ViewController: UIViewController, KeyboardDelegate {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// get an instance of the Keyboard (only the height is important)
let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
// use the delegate to communicate
keyboardView.delegate = self
// replace the system keyboard with the custom keyboard
textField.inputView = keyboardView
}
// required method for keyboard delegate protocol
func keyWasTapped(character: String) {
textField.insertText(character)
}
}
Sources
- The suggestions in @ryancrunchi's comment were helpful.
- This answer from Creating a reusable UIView with xib (and loading from storyboard)
Related
这篇关于如何使用应用内自定义键盘的按钮输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!