我們常在朋友圈、QQ 空間或微博等 App 看到這樣的操作:點擊回覆,視圖下方立刻出現一個輸入框,輸入文字後點擊發送即可。那麼這個小小的輸入框是怎麼實作的呢?
筆者也試著寫了一個小對話框,先看樣式:

主要功能有:
依功能可將該視圖大致分為三部分:分隔線(寬度為 1 的 UIView)、具 placeholder 功能的輸入框,以及按鈕。
輸入框由 UILabel+UITextView+UIImageView(藍色線)構成。UITextView 的內容長度變化可透過監聽 UITextViewTextDidChangeNotification 來判斷。先建立一個繼承 UITextView 的類別,加入 placeText 與 placeColor 屬性,並加入 UILabel 作為提示文字:
- (void)PlaceTextLabel {
if (self.placeText.length > 0) {
if (!_placeTextLab) {
CGRect frame = CGRectMake(8, 8, self.bounds.size.width - 16, 0);
_placeTextLab = [[UILabel alloc] initWithFrame:frame];
_placeTextLab.font = self.font;
_placeTextLab.backgroundColor = [UIColor clearColor];
_placeTextLab.textColor = self.placeColor;
_placeTextLab.tag = 999;
_placeTextLab.alpha = 0;
_placeTextLab.lineBreakMode = NSLineBreakByWordWrapping;
_placeTextLab.numberOfLines = 0;
[self addSubview:_placeTextLab];
}
_placeTextLab.text = self.placeText;
[_placeTextLab sizeToFit];
[_placeTextLab setFrame:CGRectMake(8, 8, CGRectGetWidth(self.bounds) - 16, CGRectGetHeight(_placeTextLab.frame))];
}
if (self.text.length == 0 && self.placeText.length > 0) {
[[self viewWithTag:999] setAlpha:1.0];
}
}
用 Photoshop 做一張單像素的藍線圖片存為 PNG,並設定 UIImageView 的顯示模式:
- (void)addLineView {
[_subLine removeFromSuperview];
_subLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subline.png"]];
[_subLine setContentMode:UIViewContentModeScaleToFill];
[self addSubline:_subLine];
}
UITextView 的設定較麻煩,需重寫初始化程式碼:
- (instancetype)initWithFrame:(CGRect)frame PlaceText:(NSString *)placeText PlaceColor:(UIColor *)placeColor {
self = [super initWithFrame:frame];
if (self) {
self.scrollEnabled = NO;
_placeText = placeText;
_placeColor = placeColor;
_Textnil = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextChange:) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
}
由於輸入框會隨文字增多而不斷增高,需把這個高度值傳給父視圖。相較於重量級的 delegate,這裡改用更輕量的 block 來傳遞。
一開始筆者想透過監聽 UITextViewTextDidChangeNotification 即時偵測高度變化,卻發現換行後高度不會立刻改變,得再輸入第二個字才會更新——這是因為高度變化前通知就已送出。所幸可以改用 UITextView 的 sizeThatFits: 方法來取得正確尺寸:
CGSize size = [self sizeThatFits:CGSizeMake(self.contentSize.width, 1000.0)];
這樣 size 就是我們要的大小了。
- (void)TextChange:(NSNotification *)notification {
if (self.text.length == 0) {
_Textnil = YES;
} else {
_Textnil = NO;
}
[self addLineView];
if (self.placeText.length == 0) return;
[UIView animateWithDuration:0.5 animations:^{
if (_Textnil) {
[[self viewWithTag:999] setAlpha:1.0];
} else {
[[self viewWithTag:999] setAlpha:0];
}
}];
}
- (void)addSubline:(UIView *)view {
CGSize size = [self sizeThatFits:CGSizeMake(self.contentSize.width, 1000.0)];
if (view) {
CGRect frame = CGRectMake(2, size.height - 3, self.bounds.size.width - 4, 3.0);
view.frame = frame;
}
[self addSubview:view];
self.viewSize(size);
}
這裡用 block 傳遞 size:先宣告一個 block 私有變數,再寫一個方法用來賦值,block 的實作由呼叫該物件的類別建立,然後像 delegate 一樣呼叫即可。這種模式在 AFNetworking 中隨處可見。
輸入框完成後即可組裝。先建立一個 UIView 子類別,它需與鍵盤高度保持一致,並監聽鍵盤滑出滑入的通知 UIKeyboardWillChangeFrameNotification 來即時改變高度:
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval boardAnimationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:boardAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGFloat keyBoardY = frame.origin.y;
CGFloat keyBoardHeigh = frame.size.height;
maxy = [UIScreen mainScreen].bounds.size.height - keyBoardHeigh;
CGRect frame = self.frame;
frame.origin.y = keyBoardY - CGRectGetHeight(self.frame);
self.frame = frame;
} completion:^(BOOL finished) {
}];
}
這裡的 maxY 是輸入框 y 方向的標準位置:無鍵盤時為螢幕高度,有鍵盤時為鍵盤的 y 值。在文字變化時即時改變高度:

- (void)TextViewDidChange:(CGSize)size {
self.frame = CGRectMake(0, self.frame.origin.y, self.frame.size.width, size.height + 6 * 2);
self.frame = CGRectMake(0, maxy - self.frame.size.height, self.frame.size.width, self.frame.size.height);
_Inputview.frame = CGRectMake(10, 6, self.frame.size.width - K_right_padding, size.height);
}
以上是 UIView 高度自適應變化的關鍵。發送按鈕與外部類別的互動同樣採用 block:
@property (nonatomic, copy) void (^sendText)(NSString *);
- (void)sendMessage:(void (^)(NSString *text))inputText {
if (inputText) {
self.sendText = inputText;
}
}
- (void)buttonClick {
[self sendInputText];
}
- (void)sendInputText {
if (_Inputview.Textnil) {
return;
} else {
if (self.sendText) {
self.sendText(_Inputview.text);
_Inputview.text = @"";
}
}
}
最後附上 sourceCode,獻給那些在 iOS 道路上一起默默前進的朋友們,共勉之。