LoginSignup
27
27

More than 5 years have passed since last update.

Swiftで「あーね」ボタンをうつと「あーね」って入力されるキーボードを作る

Posted at

普通にアプリのプロジェクトを作成する

スクリーンショット 2014-09-17 21.20.53.png

スクリーンショット 2014-09-17 21.21.45.png

そのTargetととしてCustom Keyboardを作成します

File > New > Targetを選択

スクリーンショット 2014-09-17 21.23.47.png

Custom Keyboardを選択(やっと!)

スクリーンショット 2014-09-17 21.24.04.png

プロダクト名を適当につける。

スクリーンショット 2014-09-17 21.24.46.png

コーディングしていきます

デフォルトで作られている UIInputViewController を継承した KeyboardViewController を編集していきます。

「あーね」って入力すると「あーね」しかでてこない超シンプルキーボードなので一度きれいにします。

きれいにした結果が下記です。

KeyboardViewController.swift
import UIKit

class KeyboardViewController: UIInputViewController {

    override func updateViewConstraints() {
        super.updateViewConstraints()

        // Add custom view sizing constraints here
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated
    }

    override func textWillChange(textInput: UITextInput) {
        // The app is about to change the document's contents. Perform any preparation here.
    }

    override func textDidChange(textInput: UITextInput) {
        // The app has just changed the document's contents, the document context has been updated.
    }

}

次にnibでKeyboardのviewを作ります。

作ったAneKeyboard.xibをキーボードViewにするため、KeyboardViewControllerの viewDidLoad に下記を付け加えます。

KeyboardViewController.swift
    override func viewDidLoad() {
        super.viewDidLoad()
        var v = UINib(nibName:"AneKeyboard", bundle:nil).instantiateWithOwner(self,options:nil)[0] as UIView
        self.inputView.addSubview(v)
    }

nibファイルを作って適当にボタンを配置してください。

スクリーンショット 2014-09-17 22.03.35.png

File's Owner に KeyboardViewController を選択します。
こうすることで Ctrl + drag などで簡単にbindできるようになります。

スクリーンショット 2014-09-17 21.32.42.png

「あーね」ボタン、「Next」ボタンを KeyboarViewController にバインドしてそれぞれ下記の関数を作ります。

KeyboardViewController.swift


    @IBAction func pushAne(sender: AnyObject) {
        // 「あーね」ボタン押下
    }

    @IBAction func pushNext(sender: AnyObject) {
        // 「Next」ボタン押下
    }

ボタンを押したときの処理を実装します。

KeyboardViewController.swift
    @IBAction func pushAne(sender: AnyObject) {
        // 「あーね」ボタン押下
        // こんな感じでテキストを挿入できます
        var proxy = textDocumentProxy as UITextDocumentProxy
        proxy.insertText("あーね")
    }

    @IBAction func pushNext(sender: AnyObject) {
        // 「Next」ボタン押下
        // 次のキーボードへ(これは必ず入れないといけないらしい)
        self.advanceToNextInputMode()
    }

以上で実装完了!

使ってみよう!

keyboardそのもののプロジェクトのまま立ち上げようとすると下記みたいな事になります。

???

スクリーンショット 2014-09-17 21.49.19.png

実行するプロジェクトは一番はじめに作成した被Targetなプロジェクトになります。

実行すると、そのプロジェクトが立ち上がるのでホームキー(Command + Shift + H)を押して、ホームに行きましょう。
その後、Settings -> General -> Keyboard -> Keyboards とたどっていき、「Add New Keyboard...」を選択します。

スクリーンショット 2014-09-17 21.52.42.png

画面中央の我らが「ane」を選択します。

スクリーンショット 2014-09-17 21.54.10.png

これでキーボード設定完了です。

それではSpotlightで試してみましょう。

スクリーンショット 2014-09-17 22.04.16.png

できました!

27
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
27