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/

最新文章

  1. IntelliJ IDEA 设置代码提示或自动补全的快捷键
  2. C++多线程调试和测试的注意事项
  3. 【BZOJ】【2157】旅游
  4. autoreleasepool的笔记
  5. 为什么都反对XML而支持使用json呢?
  6. unity3dWeb版文本转语音
  7. Microsoft Dynamics CRM 2013/2015 选项集的多选
  8. CentOS7.3安装zimbra8.7.11
  9. ZOOKEEPER典型应用场景解析
  10. gpu/mxGPUArray.h” Not Found
  11. 作用域public、private、protected、以及不写时的区别?
  12. matlab中输入x. 与x的区别
  13. Winform 打包 混淆 自动更新
  14. 【代码审计】五指CMS_v4.1.0 后台存在SQL注入漏洞分析
  15. [整理]win7下VS2010遇到内存不足解决方发
  16. 【Cf #178 A】Shaass and Lights(组合数)
  17. LeetCode——Consecutive Numbers
  18. SQL注入总结篇
  19. MySQL之备份恢复
  20. 文献综述十四:基于Oracle11g的超市进销存管理系统设计与实现

热门文章

  1. 模板 - 字符串 - Manacher
  2. python __builtins__ bytearray类 (7)
  3. Java--------------Mysql中时间按要求查询
  4. Spring + MyBaits java.lang.reflect.InvocationTargetException 启动日志报错
  5. sql mysql和sqlserver存在就更新,不存在就插入的写法(转)
  6. Asp.net core 框架整理
  7. html 文本溢出显示省略号 .....
  8. AtCoder Grand Contest 003 E - Sequential operations on Sequence
  9. maven-ali镜像网站setting.xml
  10. 微信小程序 插件介绍