簡體   English   中英

如何使用應用內自定義鍵盤的按鈕輸入文本

[英]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!

    override func viewDidLoad() {
        super.viewDidLoad()

        let keyboardNib = UINib(nibName: "Keyboard", bundle: nil)
        myCustomKeyboard = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView

        textField.inputView = myCustomKeyboard
    }

}

鍵盤布局是從xib文件加載的。

如何將按鈕文本輸入文本字段?

筆記:

  • 有很多關於制作自定義系統鍵盤(需要安裝)的教程,但是我只想要一個應用內鍵盤。 這些教程僅對鍵盤使用了特殊的視圖控制器,但是在這里似乎我只是在設置鍵盤視圖。
  • 我已經閱讀了“數據輸入自定義視圖”文檔。
  • 是我能找到的最接近的Stack Overflow問題,但它並沒有描述如何從按鈕中獲取文本。

更新資料

  • 本教程似乎表明有一個用於自定義輸入視圖的視圖控制器。 但是,我迷失在Objective-C代碼中。 Swift中的過程將如何?
  • 這個答案提到了UITextField符合的UIKeyInput協議,但是我該如何使用呢?
  • 如果也有內置的方式可以制作自定義應用內鍵盤,那么我真的更喜歡制作普通的自定義視圖。

我想象這樣的事情:

處理按鈕事件的新功能

func updateTextfield(sender: UIButton) {
    textField.text = (textField.text ?? "") + (sender.titleForState(.Normal) ?? "")
}

在初始化自定義鍵盤后,注冊按鈕:

myCustomKeyboard.subviews
    .filter { $0 as? UIButton != nil } // Keep the buttons only
    .forEach { ($0 as! UIButton).addTarget(self, action: "updateTextfield", forControlEvents: .TouchUpInside)}

設定

  • 制作一個包含所有密鑰的xib文件
  • 使用“自動布局”,以便無論以后將鍵盤設置為多大,按鍵的大小都將調整為正確的比例。
  • 創建一個swift與同名的文件xib文件,並將其設置為在文件所有者xib文件設置。

    在此處輸入圖片說明

    在此處輸入圖片說明 在此處輸入圖片說明

  • 將所有關鍵按鈕連接到swift文件中的IBAction方法。 (請參見下面的代碼。)

我正在使用委托模式在自定義鍵盤視圖和主視圖控制器之間進行通信。 這允許它們解耦。 可以交換多個不同的自定義鍵盤,而無需在主視圖控制器中更改詳細的實現代碼。

Keyboard.swift文件

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!)
    }

}

主視圖控制器

請注意, ViewController符合我們創建的KeyboardDelegate協議。 另外,在創建鍵盤視圖的實例時,需要設置height ,但不需要設置width 顯然,設置文本字段的inputView鍵盤視圖的寬度更新為屏幕寬度,這很方便。

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)
    }

}

資料來源

有關

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM