目錄[-]
- 一、建立一個textView
- 二、鍵盤操作
- 三、隐藏鍵盤的幾種方式
- 四、使鍵盤不擋住輸入框
- 效果圖:
- iOS7光标問題
一、建立一個textView
02 | UITextView *textView = [[[UITextView alloc] init] autorelease]; |
04 | //設定代理 需在interface中聲明UITextViewDelegate |
05 | textView.delegate = self; |
08 | textView.font = [UIFont systemFontOfSize:16]; |
11 | textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0); |
14 | textView.scrollEnabled = NO; |
17 | [textView becomeFirstResponder]; |
1 | [self.view addSubview:textView]; |
二、鍵盤操作
2 | textView.returnKeyType = UIReturnKeyDefault; |
5 | textView.keyboardType = UIKeyboardTypeDefault; |
三、隐藏鍵盤的幾種方式
個人還是認為最友善的是在鍵盤上加上一個ToolBar,在上面加上一個按鈕來隐藏鍵盤
①在鍵盤上加上隐藏按鈕
02 | UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; |
05 | [topView setBarStyle:UIBarStyleBlack]; |
07 | //定義兩個flexibleSpace的button,放在toolBar上,這樣完成按鈕就會在最右邊 |
08 | UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
10 | UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
13 | UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@ "完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)]; |
16 | NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil]; |
17 | [topView setItems:buttonsArray]; |
19 | [textView setInputAccessoryView:topView]; |
2 | - ( void )resignKeyboard { |
3 | [textView resignFirstResponder]; |
最終效果
UItextView的用法及技巧(iOS7下光标bug解決方案) 還有幾種也可隐藏鍵盤的方式
②用Enter鍵,前提是你的textView中不需要用到Enter鍵
1 | -( BOOL )textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { |
2 | if ([text isEqualToString:@ "\n" ]) |
4 | [textView resignFirstResponder]; return NO; |
③觸摸空白處隐藏鍵盤
1 | -( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
4 | [textView resignFirstResponder]; |
四、使鍵盤不擋住輸入框
在view中添加一個子view,設定此子view的tag值為1000,在此view上添加一個textView和一個發送按鈕,如下圖;我們要達到textView的鍵盤彈出時,整個View往上平移,鍵盤消失,view往下平移的效果,模拟發送短信的界面。
UItextView的用法及技巧(iOS7下光标bug解決方案) 設定textView圓角
2 | [self.textView.layer setCornerRadius:10]; |
①、在viewWillAppear中添加鍵盤監聽事件
4 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; |
7 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil]; |
②、完成①selector中鍵盤彈出keyboardDidShow:和消失keyboardDidHidden方法
在.m檔案#import後面添加
2 | #define kAnimationDuration 0.2 |
鍵盤出現
02 | -( void )keyboardDidShow:(NSNotification *)notification |
06 | NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; |
10 | [keyboardObject getValue:&keyboardRect]; |
12 | //調整放置有textView的view的位置 |
15 | [UIView beginAnimations:nil context:nil]; |
18 | [UIView setAnimationDuration:kAnimationDuration]; |
21 | [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)]; |
23 | [UIView commitAnimations]; |
鍵盤消失
02 | -( void )keyboardDidHidden |
05 | [UIView beginAnimations:nil context:nil]; |
06 | [UIView setAnimationDuration:kAnimationDuration]; |
08 | [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)]; |
09 | [UIView commitAnimations]; |
效果圖:
UItextView的用法及技巧(iOS7下光标bug解決方案)
UItextView的用法及技巧(iOS7下光标bug解決方案) iOS7光标問題
PS:有網友遇到textView在ios7上出現編輯進入最後一行時光标消失,看不到最後一行,變成盲打,stackOverFlow網站上有大神指出,是ios7本身bug,加上下面一段代碼即可(網友調試得出,在此mark一下,有問題,歡迎大神們指出)
view source print ?
01 | -( void )textViewDidChange:(UITextView *)textView { |
02 | CGRect line = [textView caretRectForPosition: |
03 | textView.selectedTextRange.start]; |
04 | CGFloat overflow = line.origin.y + line.size.height |
05 | - ( textView.contentOffset.y + textView.bounds.size.height |
06 | - textView.contentInset.bottom - textView.contentInset.top ); |
08 | // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it) |
09 | // Scroll caret to visible area |
10 | CGPoint offset = textView.contentOffset; |
11 | offset.y += overflow + 7; // leave 7 pixels margin |
12 | // Cannot animate with setContentOffset:animated: or caret will not appear |
13 | [UIView animateWithDuration:.2 animations:^{ |
14 | [textView setContentOffset:offset]; |