設計iOS中随系統鍵盤彈收和内容文字長度自适應高度的文本框
文本輸入框是多數與社交相關的app中不可或缺的一個控件,這些文本輸入框應該具備如下的功能:
1.在鍵盤為彈起時,輸入框懸浮在界面底部。
2.當鍵盤彈起時,輸入框位置上移至鍵盤上方,并且動畫應與鍵盤同步。
3.當輸入的文字超出一行時,輸入框應想用的進行高度擴充。
4.當輸入框的高度達到某一極限值時,輸入框高度不應繼續擴充,文字區域應該支援滑動。
使用autolayout布局技術加上對鍵盤的相關監聽,可以十分友善的實作上述效果。首先在xib檔案中進行相關限制的添加,如下圖:

将需要的屬性與限制對象關聯到檔案中:
//整體文本控件的高度
@IBOutlet weak var textViewHeight: NSLayoutConstraint!
//文本控件中的文字輸入控件UITestView的高度
@IBOutlet weak var textFieldHeight: NSLayoutConstraint!
//文本控件中文字輸入控件
@IBOutlet weak var ourTextField: UITextView!
//文本控件與父視圖底部的限制距離
@IBOutlet weak var textViewBottom: NSLayoutConstraint!
//文本控件
@IBOutlet weak var ourTextView: UIView!
在初始化方法中進行通知的注冊和代理的設定:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHidden:"), name: UIKeyboardWillHideNotification, object: nil)
ourTextField.delegate=self
實作通知的監聽方法如下:
//鍵盤将要展示時觸發的方法
func keyboardWillShow(noti:NSNotification){
//擷取通知資訊
let info:Dictionary = noti.userInfo!
//擷取資訊中的鍵盤尺寸和位置資訊
let value:NSValue = info[UIKeyboardFrameBeginUserInfoKey] as! NSValue
//擷取鍵盤動畫的時間資訊
let value2:NSValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSValue
let keyboardSize = value.CGRectValue()
let height = keyboardSize.height
var time:NSTimeInterval=0
value2.getValue(&time)
//重設限制
textViewBottom.constant = height
//動畫展示
UIView.animateWithDuration(time) { () -> Void in
self.view.layoutIfNeeded()
}
}
//鍵盤将要隐藏時觸發的方法
func keyboardWillHidden(noti:NSNotification){
textViewBottom.constant = 0
監聽的鍵盤狀态發送的通知中,會傳遞進來許多鍵盤資訊,可取的鍵值如下:
@available(iOS 3.2, *)
public let UIKeyboardFrameBeginUserInfoKey: String //鍵盤的初始位置尺寸 為CGRect類型的NSValue值
public let UIKeyboardFrameEndUserInfoKey: String // 鍵盤的末位位置尺寸 為CGRect類型的NSValue值
@available(iOS 3.0, *)
public let UIKeyboardAnimationDurationUserInfoKey: String // 鍵盤動畫時間 double類型的NSValue
public let UIKeyboardAnimationCurveUserInfoKey: String // 鍵盤動畫效果 (UIViewAnimationCurve)枚舉類型的NSNumber值
@available(iOS 9.0, *)
public let UIKeyboardIsLocalUserInfoKey: String //與多任務相關 判斷鍵盤是否屬于目前應用 iOS9後可用
可以監聽的與鍵盤相關資訊的通知有如下幾種:
public let UIKeyboardWillShowNotification: String//鍵盤将要出現
public let UIKeyboardDidShowNotification: String//鍵盤已經出現
public let UIKeyboardWillHideNotification: String//鍵盤将要隐藏
public let UIKeyboardDidHideNotification: String//鍵盤已經隐藏
@available(iOS 5.0, *)
public let UIKeyboardWillChangeFrameNotification: String//鍵盤frame将要改變
public let UIKeyboardDidChangeFrameNotification: String//鍵盤frame已經改變
還需要實作當輸入框文字長度改變時的回調方法如下:
func textViewDidChange(textView: UITextView) {
let height = textView.contentSize.height
if height <= 37 {
textFieldHeight.constant = 37
textViewHeight.constant = 53
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutIfNeeded()
})
return
//臨界值
}else if height<100 {
textFieldHeight.constant = height
textViewHeight.constant = height+16
}else{
textFieldHeight.constant = 100
textViewHeight.constant = 116
上面代碼是實作可自适應高度和位置的文本輸入框控件的核心代碼,效果圖下圖: