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. c语言sscanf总结

    1函数原型 int scanf(const char *format,[,argument]...) extern int sscanf(_const char*_restrict_s,const c ...

  2. LightOJ 1140 How Many Zeroes? (数位DP)

    题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...

  3. hdoj5832【模拟】

    主要还是一个10001的倍数的问题: 队友的思路: 01 1个数*10001,最后四位是这个数的后四位 比如 521456 10001 521456 521456 9 5215081456 从后面fo ...

  4. windows 远程连接 密码正确但是无法登陆,提示证书不正确

    问题: windows8.1 远程连接 windows8 进入输入用户名密码环节,用户名,密码都正确,但是无法登陆 连接时,下方写着 域名 MicrosoftAccount 解决: 在输入密码后,点击 ...

  5. LuoguP1370 Charlie的云笔记序列 【dp】By cellur925

    题目传送门 题目大意:给你一个序列,求出它所有区间的本质不同的子序列个数.(空序列也算作本质不同),数据范围$1e5$. 我们肯定是不能一个个枚举区间的...而且这个复杂度下,也就大概$O(n)$或$ ...

  6. Zeppelin的入门使用系列之创建新的Notebook

    前期博客 hadoop-2.6.0.tar.gz + spark-1.6.1-bin-hadoop2.6.tgz + zeppelin-0.5.6-incubating-bin-all.tgz(mas ...

  7. C++中的定位放置new(placement new)

    一般来说,使用new申请空间时,是从系统的“堆”(heap)中分配空间.申请所得的空间的位置时根据当时的内存的实际使用情况决定的.但是,在某些特殊情况下,可能需要在程序员指定的特定内存创建对象,这就是 ...

  8. FlashFXP Registration

    -------- FlashFXP Registration Data START --------FLASHFXPuQBW1wi5uQAAAACvW7cJKQXzmx8Eu6ikXL4LbrYQHZ ...

  9. AtCoder Beginner Contest 058 ABCD题

    A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...

  10. php的iconv函数中utf8与utf-8的差异

    开发中遇到一个奇怪的问题:报错如下: iconv() [<a href='function.iconv'>function.iconv</a>] : Wrong charset ...