看過小編前面的文章的小伙伴們應(yīng)該已經(jīng)發(fā)現(xiàn)了,在UITextField中期揪,小編簡(jiǎn)單的介紹過一丟丟關(guān)于UIKeyboard的鍵盤類型。在這篇文章中,我們來(lái)詳細(xì)的看一下UIKeyboard的相關(guān)知識(shí)吧苍苞。
一、鍵盤的相關(guān)屬性
1.鍵盤8種類型
textF.keyboardType = UIKeyboardType.Default //系統(tǒng)默認(rèn)的虛擬鍵盤
textF.keyboardType = UIKeyboardType.ASCIICapable //顯示英文字母的虛擬鍵盤
textF.keyboardType = UIKeyboardType.NumbersAndPunctuation //顯示數(shù)字和標(biāo)點(diǎn)的虛擬鍵盤
textF.keyboardType = UIKeyboardType.URL //顯示便于輸入數(shù)字的虛擬鍵盤
textF.keyboardType = UIKeyboardType.NumberPad //顯示便于輸入數(shù)字的虛擬鍵盤
textF.keyboardType = UIKeyboardType.PhonePad //顯示便于撥號(hào)呼叫的虛擬鍵盤
textF.keyboardType = UIKeyboardType.NamePhonePad //顯示便于聊天撥號(hào)的虛擬鍵盤
textF.keyboardType = UIKeyboardType.EmailAddress //顯示便于輸入Email的虛擬鍵盤
textF.keyboardType = UIKeyboardType.DecimalPad //顯示用于輸入數(shù)字和小數(shù)點(diǎn)的虛擬鍵盤
textF.keyboardType = UIKeyboardType.Twitter //顯示方便些Twitter的虛擬鍵盤
textF.keyboardType = UIKeyboardType.WebSearch //顯示便于在網(wǎng)頁(yè)上書寫的虛擬鍵盤
2.鍵盤的外觀
textF.keyboardAppearance = .Default //默認(rèn)外觀:淺灰色
textF.keyboardAppearance = .Dark //黑色
textF.keyboardAppearance = .Light //亮色狼纬,與Default很相似
3.鍵盤的回車鍵
textF.returnKeyType = .Default //默認(rèn):灰色按鈕羹呵,標(biāo)有Return
textF.returnKeyType = .Continue // 標(biāo)有Continue的藍(lán)色按鈕
textF.returnKeyType = .Done //標(biāo)有Done的藍(lán)色按鈕
textF.returnKeyType = .EmergencyCall //緊急呼叫按鈕
textF.returnKeyType = .Go //標(biāo)有Go的藍(lán)色按鈕
textF.returnKeyType = .Google //標(biāo)有Google的藍(lán)色按鈕,用于搜索
textF.returnKeyType = .Join //標(biāo)有Join的藍(lán)色按鈕
textF.returnKeyType = .Next //標(biāo)有Next的藍(lán)色按鈕
textF.returnKeyType = .Route //標(biāo)有Route的藍(lán)色按鈕
textF.returnKeyType = .Search //標(biāo)有Search的藍(lán)色按鈕
textF.returnKeyType = .Send //標(biāo)有Send的藍(lán)色按鈕
textF.returnKeyType = .Yahoo //標(biāo)有Yahoo!的藍(lán)色按鈕疗琉,用于搜索
4.鍵盤的大寫屬性
textF.autocorrectionType = .Default //默認(rèn)
textF.autocorrectionType = .No //不自動(dòng)更正
textF.autocorrectionType = .Yes //自動(dòng)更正
5.安全性
當(dāng)我們想輸入密碼的時(shí)候當(dāng)然需要安全顯示啦冈欢,鍵盤也為我們提供了這樣一個(gè)屬性。
textF.secureTextEntry = true
二盈简、鍵盤的事件分析
class ViewController: UIViewController,UITextFieldDelegate {
var textField:UITextField?
//Mark:life cycle
override func viewDidLoad() {
super.viewDidLoad()
textField = UITextField(frame: CGRectMake(50, 50, 150, 30))
textField?.backgroundColor = UIColor.cyanColor()
textField?.textColor = UIColor.blackColor()
textField?.placeholder = "請(qǐng)輸入用戶名"
textField?.delegate = self
view.addSubview(textField!)
}
override func viewWillDisappear(animated: Bool) {
if self.textField != nil {
let center = NSNotificationCenter.defaultCenter()
center.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
}
//Mark:Delegate
func textFieldDidBeginEditing(textField: UITextField) {
//監(jiān)聽鍵盤彈出通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
//監(jiān)聽鍵盤收回通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
}
func textFieldDidEndEditing(textField: UITextField) {
self.view.endEditing(true)
}
//鍵盤顯示
func keyboardWillShow(notification:NSNotification) {
let textMaxY = CGRectGetMaxY(self.textField!.frame)
let keyboardH : CGFloat = (notification.userInfo![UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height)!
let keyboardY : CGFloat = self.view.frame.size.height - keyboardH
var duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue
if duration < 0.0 {
duration = 0.25
}
UIView.animateWithDuration(duration!) { () -> Void in
if (textMaxY > keyboardY) {
self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - textMaxY - 10)
}else{
self.view.transform = CGAffineTransformIdentity
}
}
}
//鍵盤隱藏
func keyboardWillHide(notification:NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue
UIView.animateWithDuration(duration!) { () -> Void in
self.view.transform = CGAffineTransformIdentity
}
}
}
實(shí)現(xiàn)方法二:
func keyboardWillShow(note: NSNotification) {
let userInfo = note.userInfo!
let keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let deltaY = keyBoardBounds.size.height - 200
let animations:(() -> Void) = {
if deviceTypeIphone5() {
//鍵盤的偏移量
self.textView.transform = CGAffineTransform(translationX: 0 , y: -deltaY)
}
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
}else{
animations()
}
}
func keyboardWillHidden(note: NSNotification) {
self.textView.transform = CGAffineTransform(translationX: 0 , y: 0)
}
在這里對(duì)代碼進(jìn)行簡(jiǎn)單的解釋:
我們都知道凑耻,當(dāng)焦點(diǎn)到可輸入的控件上(一般指UITextField)時(shí),鍵盤會(huì)自動(dòng)顯示柠贤,并且觸發(fā)UIKeyboardWillShowNotification通知香浩;當(dāng)焦點(diǎn)離開可輸入的控件時(shí),鍵盤會(huì)自動(dòng)隱藏臼勉,并且觸發(fā)UIKeyboardWillHideNotification通知邻吭。所以,要注冊(cè)這兩種通知事件坚俗。
關(guān)于notification.userInfo的作用就是返回許多有用的參數(shù)镜盯,在這里我用的是UIKeyboardAnimationDurationUserInfoKey,查看內(nèi)部我們可以發(fā)現(xiàn) UIKeyboardAnimationDurationUserInfoKey = "0.25";