UITextView与UITextfield的区别
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的区别的更多相关文章
- 用UITextView模拟UITextField的placeHolder
		用UITextView模拟UITextField的placeHolder 效果: 源码: // // ViewController.m // TextView // // Created by You ... 
- UITextView 和 UITextField限制字符数和表情符号
		UITextField限制字符数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)r ... 
- UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样
		本文转载至 http://blog.csdn.net/hengshujiyi/article/details/9086093- (void)initFeedBackViews { //设置页面的背景颜 ... 
- UITextView模拟UITextField 设置Placeholder属性 --董鑫
		由于最近有用到输入框,刚开始考虑的是UITextField,因为它在没有输入的时候可以有提示的Placeholder更能,很人性化,但UITextField只能单行输入,不能跳行,对于一些强迫症的亲来 ... 
- UITextView 点击添加文字 光标处于最后方
		#import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @end @im ... 
- UI:UITextView
		#import "MainViewController.h" @interface MainViewController () <UITextViewDelegate> ... 
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
		博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ... 
- UITextView/UITextField检测并过滤Emoji表情符号
		UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ... 
- UI控件(UITextView)
		@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //UITextView与UITextField主要 ... 
随机推荐
- 安装GitLab出现ruby_block[supervise_redis_sleep] action run
			在卸载gitlab然后再次安装执行sudo gitlab-ctl reconfigure的时候往往会出现:ruby_block[supervise_redis_sleep] action run,会一 ... 
- (水题)Codeforces - 327C - Magic Five
			https://codeforces.com/problemset/problem/327/C 因为答案可以有前导零,所以0和5一视同仁.每个小节内,以排在第 $i$ 个的5为结尾的序列即为在前面 $ ... 
- Ecliplse 指定JRE
			http://blog.csdn.net/hongweigg/article/details/9987649 在Eclipse启动的过程中,它会去找系统环境变量设置的JRE_HOME或JDK_HOME ... 
- 黑客攻防技术宝典web实战篇:定制攻击自动化习题
			猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 指出使用自动技巧在应用程序中枚举标识符时用到的 3 个标识符“触点”. (a) HTTP ... 
- [问题][已解决] 并发场景下 "mysql: too many connections" 原因
			问题出现是这样的,用node写爬虫, 之前每条数据都是await插入,并且是阻塞的,后来改成了非阻塞,可以并行插入操作,结果一直找不到原因. 后来在日志中找到了 too many connection ... 
- Spring自动扫描注解类的冲突问题
			原文地址:http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html Spring MVC项目中通常会有二个配置文件,spring-s ... 
- jsp 接收汉字参数乱码
			这两天跟汉字问题较上劲了,真是考验基本功 1. ${param.userName} 乱码 解决方法: <%String name = (String)request.getParameter( ... 
- angular 2 angular quick start Could not find HammerJS
			Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ... 
- 虚方法virtual详解
			虚方法virtual详解 从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在 ... 
- C#之九大视图
			本节向大家介绍一下有关UML视图方面的内容,UML视图共有9种,它们之间有什么区别和联系呢,下面就让我们一起来学习吧,相信通过本节的介绍你一定会有不少收获. UML视图 UML总共提供了9种视图,这些 ... 
