iOS自定義鍵盤實(shí)戰(zhàn)
0x01 來龍去脈
在iOS8之前,iOS系統(tǒng)的輸入法只能使用蘋果官方提供的輸入法檩小。
對(duì)于中文來說浅妆,官方的輸入法并不好用穿仪,或者說不夠好用,詞庫框舔,聯(lián)想伴栓,云輸入等都沒有或者和搜狗輸入法,百度輸入法等有中國特色的輸入法相比有一定的差距雨饺。
部分用戶因?yàn)檩斎敕ǖ脑蚯澹x擇了安卓等其他系統(tǒng),或者選擇了越獄额港。
iOS自定義鍵盤是iOS8系統(tǒng)新推出的功能饺窿,允許開發(fā)者開發(fā)第三方鍵盤。眾人拾柴火焰高移斩,功能的開放肚医,為iOS用戶體驗(yàn)的提示必然帶來更大的推動(dòng)绢馍。
0x02 輸入法的基本結(jié)構(gòu)
輸入法的項(xiàng)目可以使用普通的project模版,但是需要增加一個(gè)自定義鍵盤的Target肠套,所有的人機(jī)交互舰涌,都在項(xiàng)目生成的UIInputViewController一個(gè)子類里實(shí)現(xiàn)。關(guān)鍵的要素如下:
- 一個(gè)Xcode的Project
- 一個(gè)自定義鍵盤的Target
- 一個(gè)UIInputViewController的子類
- 必須提供一個(gè)明顯的切換到其他輸入法的按鈕
0x03 功能需求描述
- 鍵盤上提供一個(gè)按鈕你稚,點(diǎn)擊之后輸入“一支紅杏出墻來”
0x04 交互序列
0x05 實(shí)現(xiàn)
0x0501. 建立一個(gè)單視圖的project
0x0502. 增加一個(gè)自定義鍵盤的target(Custom Keyboard)
添加之后瓷耙,會(huì)看到有一個(gè)文件:KeyboardViewController,這是我們的主戰(zhàn)場
0x0503. 編輯代碼
代碼的內(nèi)容很簡單刁赖,創(chuàng)建兩個(gè)按鈕搁痛,一個(gè)點(diǎn)擊之后,切換輸入法宇弛,一個(gè)點(diǎn)擊之后鸡典,向當(dāng)前輸入控件里添加”一枝紅杏出墻來"
//
// KeyboardViewController.swift
// keyboard
//
// Created by qi on 15/12/3.
// Copyright ? 2015年 im.windgo. All rights reserved.
//
import UIKit
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton! //切換輸入法
@IBOutlet var yizhihongxingButton: UIButton! //輸入一支紅杏出墻來的按鈕
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
//創(chuàng)建切換到另一個(gè)鍵盤的按鈕
self.nextKeyboardButton = UIButton(type: .System)
self.nextKeyboardButton.setTitle(NSLocalizedString("切換鍵盤", comment: "切換到下一個(gè)鍵盤的按鈕"), forState: .Normal)
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
yizhihongxingButton=UIButton()
yizhihongxingButton.backgroundColor=UIColor.magentaColor()
yizhihongxingButton.setTitle("一支紅杏出墻來!", forState: UIControlState.Normal)
yizhihongxingButton.frame=CGRectMake(0, 40, UIScreen.mainScreen().bounds.width, 100)
yizhihongxingButton.addTarget(self, action: "onInputText", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(yizhihongxingButton)
}
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.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
func onInputText(){
self.textDocumentProxy.insertText("一支紅杏出墻來枪芒!")
}
0x05004. 運(yùn)行代碼
-
選擇運(yùn)行的target
-
執(zhí)行會(huì)讓你選擇在哪個(gè)應(yīng)用里使用鍵盤彻况,選擇Safari瀏覽器就可以了
選擇地址欄,進(jìn)入輸入狀態(tài)舅踪,可以看到鍵盤疗垛,點(diǎn)擊會(huì)輸入“一支紅杏出墻來!”