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. P5167 xtq的神笔

    传送门 题解 倍增也好二分也好,果然复杂度只要和\(\log\)插上关系就没我啥事了-- 首先由一个显而易见然而我完全没有发现的结论,设\(calc(l,r)\)表示区间\([l,r]\)的\(or\ ...

  2. 【POJ - 2664】Prerequisites? (排序+查找)

    Prerequisites? 原文是English,这里直接就写中文吧 题意简述 k:已经选择的科目数:m:选择的科目类别:c:能够选择的科目数.r:要求最少选择的科目数量 在输入的k和m以下的一行是 ...

  3. js对象注册清除

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  4. WIN7 64位操作系统 无法找到Access驱动

    为了更充分的利用硬件资源,我想很多人都开使用64位操作系统了,同时你可以也发现了在64位操作系统上ODBC的驱动找不到了,所以ODBC的东西都没法用了. 因为2007以前版本的Office只有32位版 ...

  5. 优先队列 POJ 3253 Fence Repair

    题目传送门 题意:一块木板按照某个顺序切成a[1], a[2]...a[n]的长度,每次切都会加上该两段木板的长度,问选择什么顺序切能使得累加和最小 分析:网上说这是哈夫曼树.很容易想到先切掉最长的, ...

  6. selenium2+python自动化1-操作浏览器

    随着测试行业的发展,现在不论在找工作还是在实际的工作中,对要求掌握自动化越来越普遍,在这里就记录一下一些入门的知识,希望对阅读者能有一些帮助吧!好哒,步入正题,这一篇主要记录下如何用Python调用w ...

  7. Vue 页面加载闪现代码问题

    CSS中 [v-cloak] { display: none; } HTML中 <div v-cloak> {{ message }} </div> 显示代码主要是{{}}这个 ...

  8. Windows API函数大全四

    10. API之硬件与系统函数 ActivateKeyboardLayout 激活一个新的键盘布局.键盘布局定义了按键在一种物理性键盘上的位置与含义 Beep 用于生成简单的声音 CharToOem ...

  9. iOS面试题之runloop

    本文围绕以下几个部分展开对runloop的叙述. 1.runloop是什么/runloop的概念? 2.NSRunLoop 和 CFRunLoopRef? 3.runloop和线程的关系? 4.run ...

  10. JVM(HotSpot)7种垃圾收集器

    JVM(HotSpot)7种垃圾收集器 7种垃圾收集器作用于不同的分代,如果两个收集器之间存在连续,就说明他们可以搭配使用. 从JDK1.3到现在,从Serial收集器->Parallel收集器 ...