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的区别的更多相关文章

  1. 用UITextView模拟UITextField的placeHolder

    用UITextView模拟UITextField的placeHolder 效果: 源码: // // ViewController.m // TextView // // Created by You ...

  2. UITextView 和 UITextField限制字符数和表情符号

    UITextField限制字符数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)r ...

  3. UITextView和UITextField的placeholder,键盘隐藏,键盘换行变完成字样

    本文转载至 http://blog.csdn.net/hengshujiyi/article/details/9086093- (void)initFeedBackViews { //设置页面的背景颜 ...

  4. UITextView模拟UITextField 设置Placeholder属性 --董鑫

    由于最近有用到输入框,刚开始考虑的是UITextField,因为它在没有输入的时候可以有提示的Placeholder更能,很人性化,但UITextField只能单行输入,不能跳行,对于一些强迫症的亲来 ...

  5. UITextView 点击添加文字 光标处于最后方

    #import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @end @im ...

  6. UI:UITextView

    #import "MainViewController.h" @interface MainViewController () <UITextViewDelegate> ...

  7. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

  8. UITextView/UITextField检测并过滤Emoji表情符号

    UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...

  9. UI控件(UITextView)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //UITextView与UITextField主要 ...

随机推荐

  1. CodeForces 712C Memory and De-Evolution (贪心+暴力)

    题意:现在有一个长度为 x 的正三角形,每次可以把一条边减小,然后用最少的时间变成长度为 y 的正三角形. 析:一开始,正着想,然后有一个问题,就是第一次减小多少才能最快呢?这个好像并不好确定,然后我 ...

  2. Jquery禁用网页右键菜单

    $(function(){ $(document).bind("contextmenu",function(e){ return false; }); });

  3. pycharm 断点调试

    转自; https://blog.csdn.net/chenggong2dm/article/details/9368641 PyCharm 作为IDE,断点调试是必须有的功能.否则,我们还真不如用纯 ...

  4. 分布式集群环境下,如何实现session共享三(环境搭建)

    这是分布式集群环境下,如何实现session共享系列的第三篇.在上一篇:分布式集群环境下,如何实现session共享二(项目开发)中,准备好了一个通过原生态的servlet操作session的案例.本 ...

  5. Queue Sequence HDU - 4441

    码力不行啊... 错误记录: 171行后面对find2的使用错误,原来写的是p=find2(rt,p1),然后再加上一句能过样例但很假的特判 事实上,现在是要寻找最大的j,使得d2[1..j-1]=p ...

  6. UVa 11437 (梅涅劳斯定理) Triangle Fun

    题意: 给出三角形ABC顶点的坐标,DEF分别是三边的三等分点.求交点所形成的三角形PQR的面积. 分析: 根据梅涅劳斯定理,我们得到: ,解得 另外还有:,解得 所以AR : RP : PD = 3 ...

  7. Objective-C和 C++ 混编的要点(转)

    Using C++ With Objective-C苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++.有 ...

  8. Python实现决策树C4.5算法

    为什么要改进成C4.5算法 原理 C4.5算法是在ID3算法上的一种改进,它与ID3算法最大的区别就是特征选择上有所不同,一个是基于信息增益比,一个是基于信息增益. 之所以这样做是因为信息增益倾向于选 ...

  9. 搭建Sql Server AlwaysOn 视频教程

    本人搭建关于Sql Server 2012 AlwaysOn 的视频系列教程. 教程一: 观看及下载地址:https://pan.baidu.com/s/1QAvhgFTSyvpR5xQZbKHO3w ...

  10. Asp.net 字符(一)

    1.字母大小写处理 private string GetChangedStr(string oldStr, strType type) { string newStr = ""; ...