天天看點

UItextView的用法及技巧(iOS7下光标bug解決方案)

目錄[-]

  • 一、建立一個textView
  • 二、鍵盤操作
  • 三、隐藏鍵盤的幾種方式
  • 四、使鍵盤不擋住輸入框
  • 效果圖:
  • iOS7光标問題

一、建立一個textView

01

//初始化

02

UITextView *textView = [[[UITextView alloc] init] autorelease];

03

04

//設定代理 需在interface中聲明UITextViewDelegate

05

textView.delegate = self;

06

07

//字型大小

08

textView.font = [UIFont systemFontOfSize:16];

09

10

//添加滾動區域

11

textView.contentInset = UIEdgeInsetsMake(-11, -6, 0, 0);

12

13

//是否可以滾動

14

textView.scrollEnabled = NO;

15

16

//獲得焦點

17

[textView becomeFirstResponder];

1

[self.view addSubview:textView];

二、鍵盤操作

1

//傳回鍵的類型

2

textView.returnKeyType = UIReturnKeyDefault;

3

4

//鍵盤類型

5

textView.keyboardType = UIKeyboardTypeDefault;

三、隐藏鍵盤的幾種方式

個人還是認為最友善的是在鍵盤上加上一個ToolBar,在上面加上一個按鈕來隐藏鍵盤

①在鍵盤上加上隐藏按鈕

01

//定義一個toolBar

02

UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];

03

04

//設定style

05

[topView setBarStyle:UIBarStyleBlack];

06

07

//定義兩個flexibleSpace的button,放在toolBar上,這樣完成按鈕就會在最右邊

08

UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

09

10

UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];

11

12

//定義完成按鈕

13

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@

"完成"

style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];

14

15

//在toolBar上加上這些按鈕

16

NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];    

17

[topView setItems:buttonsArray];

18

19

[textView setInputAccessoryView:topView];

1

//隐藏鍵盤

2

- (

void

)resignKeyboard {

3

[textView resignFirstResponder];

4

}

最終效果

UItextView的用法及技巧(iOS7下光标bug解決方案)

還有幾種也可隐藏鍵盤的方式

②用Enter鍵,前提是你的textView中不需要用到Enter鍵

1

-(

BOOL

)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

2

if

([text isEqualToString:@

"\n"

])

3

{

4

[textView resignFirstResponder]; 

return

NO;

5

}

6

return

YES;

7

}

③觸摸空白處隐藏鍵盤

1

-(

void

)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

2

{

3

//隐藏鍵盤

4

[textView resignFirstResponder];

5

}

四、使鍵盤不擋住輸入框

    在view中添加一個子view,設定此子view的tag值為1000,在此view上添加一個textView和一個發送按鈕,如下圖;我們要達到textView的鍵盤彈出時,整個View往上平移,鍵盤消失,view往下平移的效果,模拟發送短信的界面。

UItextView的用法及技巧(iOS7下光标bug解決方案)

設定textView圓角

1

//設定textView圓角

2

[self.textView.layer setCornerRadius:10];

①、在viewWillAppear中添加鍵盤監聽事件

1

//添加鍵盤的監聽事件

2

3

//注冊通知,監聽鍵盤彈出事件

4

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

5

6

//注冊通知,監聽鍵盤消失事件

7

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];

②、完成①selector中鍵盤彈出keyboardDidShow:和消失keyboardDidHidden方法

    在.m檔案#import後面添加

1

//動畫時間

2

#define kAnimationDuration 0.2

3

//view高度

4

#define kViewHeight 56

鍵盤出現

01

// 鍵盤彈出時

02

-(

void

)keyboardDidShow:(NSNotification *)notification

03

{

04

05

//擷取鍵盤高度

06

NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];

07

08

CGRect keyboardRect;

09

10

[keyboardObject getValue:&keyboardRect];

11

12

//調整放置有textView的view的位置

13

14

//設定動畫

15

[UIView beginAnimations:nil context:nil];

16

17

//定義動畫時間

18

[UIView setAnimationDuration:kAnimationDuration];

19

20

//設定view的frame,往上平移

21

[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];

22

23

[UIView commitAnimations];

24

25

}

鍵盤消失

01

//鍵盤消失時

02

-(

void

)keyboardDidHidden

03

{

04

//定義動畫

05

[UIView beginAnimations:nil context:nil];

06

[UIView setAnimationDuration:kAnimationDuration];

07

//設定view的frame,往下平移

08

[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];

09

[UIView commitAnimations];

10

}

效果圖:

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 );

07

if

( overflow > 0 ) {

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];

15

}];

16

}

17

}