IOS中的UITextView和UITextField都是文本输入控件并都能够调用系统键盘。本次特酷把介绍UITextView和UITextField的区别。
简单来说,UITextView和UITextField最大的区别是:UITextView支持多行输入,而UITextField只能单行输入。实际上,UITextView继承自UIScrollView,UITextField继承自UIView[UIControl]。在使用上我们完全可以把UITextView看作是UITextField的加强版。下面是他们各自的代理协议,可以看出,他们的代理协议很相似,事实上用法也相通。
折叠展开C/C++
Code复制内容到剪贴板UITextView的协议UITextViewDelegate:
@protocol
UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
- (BOOL)textViewShouldBeginEditing:(UITextView
*)textView;
- (BOOL)textViewShouldEndEditing:(UITextView
*)textView;
- (void)textViewDidBeginEditing:(UITextView
*)textView;
- (void)textViewDidEndEditing:(UITextView
*)textView;
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
-
(void)textViewDidChangeSelection:(UITextView *)textView;
@end
UITextField的协议UITextFieldDelegate:
@protocol UITextFieldDelegate
<NSObject>
@optional
-
(BOOL)textFieldShouldBeginEditing:(UITextField
*)textField; // return NO to disallow
editing.
- (void)textFieldDidBeginEditing:(UITextField
*)textField; //
became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField
*)textField; // return YES
to allow editing to stop and to resign first responder status. NO to disallow
the editing session to end
- (void)textFieldDidEndEditing:(UITextField
*)textField;
// may be called if forced even if shouldEndEditing returns NO (e.g. view
removed from window) or endEditing:YES called
-
(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString
*)string; // return NO to not change text
-
(BOOL)textFieldShouldClear:(UITextField
*)textField;
// called when clear button pressed. return NO to ignore (no
notifications)
- (BOOL)textFieldShouldReturn:(UITextField
*)textField;
// called when ‘return‘ key pressed. return NO to ignore.
@end
在这里值得一提的是IOS中常常需要有限制用户输入字数的要求,我们可以如下处理:
1,UITextView
我们可以在-
(void)textViewDidChange:(UITextView *)textView[检测到输入变化的时候执行]和
-
(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text[超过一定字数返回NO即可]
2,UITextField
简单的方法只能在 -
(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString
*)string; 方法中处理。
本文来源于特酷吧http://www.tekuba.net/, 原文地址:http://www.tekuba.net/program/272/
UITextView与UITextfield的区别,布布扣,bubuko.com
原文:http://www.cnblogs.com/yulang314/p/3571008.html