設(shè)置自定義鍵盤與系統(tǒng)鍵盤切換判斷邏輯:
composeTextView : 自定義的TextView(繼承自UITextView)
bottomToolBar : 自定義的輔助工具條(繼承自UIView)
控制器下切換鍵盤:
// 切換鍵盤方法
func switchKeyboard () -> Void {
// 系統(tǒng)鍵盤 點(diǎn)擊切換按鈕后,切換到自定義鍵盤
if composeTextView.inputView == nil {
composeTextView.inputView = UIButton(type: UIButtonType.ContactAdd)
} else {
// 自定義鍵盤 點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤
composeTextView.inputView = nil
}
// 刷新inputView
composeTextView.reloadInputViews()
// 成為第一響應(yīng)者
composeTextView.becomeFirstResponder()
}
自定義鍵盤(button演示):
keyboard_01.png
系統(tǒng)鍵盤:
keyboard_02.png
自定義鍵盤ToolBar按鈕圖片切換:
在bottomToolBar類中聲明一個Bool類型的對外屬性,用來作為按鈕狀態(tài)切換的標(biāo)識
如果isEmoticon == true 代表當(dāng)前為自定義鍵盤 button 顯示image為鍵盤
如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤 button 顯示image為自定義鍵盤
代碼:
// 表情鍵盤按鈕標(biāo)識 (供外界設(shè)置)
/*
如果isEmoticon == true 代表當(dāng)前為自定義鍵盤 button顯示image為系統(tǒng)鍵盤
如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤 button顯示image為自定義鍵盤
*/
var isEmoticon: Bool = false{
didSet{
if isEmoticon {
//圖標(biāo)顯示系統(tǒng)鍵盤(當(dāng)前是自定義鍵盤)
emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
} else {
//圖標(biāo)顯示自定義鍵盤(當(dāng)前是系統(tǒng)鍵盤)
emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
}
}
}
// 表情鍵盤按鈕 因?yàn)樾枰獙R進(jìn)行設(shè)置,所以需要抽取出來
var emoticonButton: UIButton?
在控制器中點(diǎn)擊自定義ToolBar內(nèi)按鈕切換鍵盤時,設(shè)置標(biāo)識(通過閉包監(jiān)聽Button)
// 切換鍵盤
func switchKeyboard () -> Void {
// 系統(tǒng)鍵盤 點(diǎn)擊切換按鈕后,切換到自定義鍵盤
if composeTextView.inputView == nil {
composeTextView.inputView = keyboard
bottomToolBar.isEmoticon = true
} else {
// 自定義鍵盤 點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤
composeTextView.inputView = nil
bottomToolBar.isEmoticon = false
}
// 刷新inputView
composeTextView.reloadInputViews()
// 成為第一響應(yīng)者
composeTextView.becomeFirstResponder()
}
顯示自定義鍵盤時,圖標(biāo)顯示為系統(tǒng)鍵盤
emo.png
顯示系統(tǒng)鍵盤時,圖標(biāo)顯示為自定義鍵盤
key.png