062在文本输入框中设置一个清空按钮

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController<UITextFieldDelegate>
4 @property (strong, nonatomic) NSMutableArray *mArrTextField;
5
6 @end

ViewController.m

 1 #import "ViewController.h"
 2
 3 @interface ViewController ()
 4 - (void)layoutUI;
 5 - (UITextField *)newTextField:(UITextFieldViewMode)clearButtonMode frame:(CGRect)rect;
 6 - (void)hideKeyboard:(id)sender;
 7 @end
 8
 9 @implementation ViewController
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12
13     [self layoutUI];
14
15     UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
16     [self.view addGestureRecognizer:tapGestureRecognizer];
17 }
18
19 - (void)didReceiveMemoryWarning {
20     [super didReceiveMemoryWarning];
21     // Dispose of any resources that can be recreated.
22 }
23
24 #pragma mark - Private Methods
25 /**
26  *  隐藏键盘
27  *
28  *  @param sender 事件触发者
29  */
30 - (void)hideKeyboard:(id)sender {
31     NSLog(@"Tap Gesture Recognizer,去掉某些控件作为第一响应器");
32     for (UITextField *txtF in _mArrTextField) {
33         [txtF resignFirstResponder];
34     }
35 }
36
37 - (void)layoutUI {
38     _mArrTextField = [[NSMutableArray alloc] initWithCapacity:4];
39
40     CGRect rectTemp = CGRectMake(20, 40, 320, 40);
41     UITextField *txtFTemp = [self newTextField:UITextFieldViewModeNever frame:rectTemp];
42     [self.view addSubview:txtFTemp];
43     [_mArrTextField addObject:txtFTemp];
44
45     rectTemp.origin.y += 50;
46     txtFTemp = [self newTextField:UITextFieldViewModeWhileEditing frame:rectTemp];
47     [self.view addSubview:txtFTemp];
48     [_mArrTextField addObject:txtFTemp];
49
50     rectTemp.origin.y += 50;
51     txtFTemp = [self newTextField:UITextFieldViewModeUnlessEditing frame:rectTemp];
52     [self.view addSubview:txtFTemp];
53     [_mArrTextField addObject:txtFTemp];
54
55     rectTemp.origin.y += 50;
56     txtFTemp = [self newTextField:UITextFieldViewModeAlways frame:rectTemp];
57     [self.view addSubview:txtFTemp];
58     [_mArrTextField addObject:txtFTemp];
59 }
60
61 - (UITextField *)newTextField:(UITextFieldViewMode)clearButtonMode frame:(CGRect)rect {
62     UITextField *txtFMessage = [[UITextField alloc] initWithFrame:rect];
63     txtFMessage.textAlignment = NSTextAlignmentLeft;
64     txtFMessage.textColor = [UIColor colorWithRed:0.433 green:0.829 blue:1.000 alpha:1.000];
65     txtFMessage.borderStyle = UITextBorderStyleRoundedRect;
66
67     txtFMessage.delegate = self;
68     txtFMessage.clearButtonMode = clearButtonMode;
69     switch (clearButtonMode) {
70         case UITextFieldViewModeNever:
71             txtFMessage.text = @"UITextFieldViewModeNever"; //不显示清空按钮;默认值
72             break;
73         case UITextFieldViewModeWhileEditing:
74             txtFMessage.text = @"UITextFieldViewModeWhileEditing";  //文本内容不为空时,且在编辑状态(即获得焦点)时显示清空按钮
75             break;
76         case UITextFieldViewModeUnlessEditing:
77             txtFMessage.text = @"UITextFieldViewModeUnlessEditing";  //文本内容不为空时,且不在编辑状态(即失去焦点)时显示清空按钮
78             break;
79         case UITextFieldViewModeAlways:
80             txtFMessage.text = @"UITextFieldViewModeAlways"; //文本内容不为空时,获得焦点与失去焦点都显示清空按钮
81             break;
82     }
83     return txtFMessage;
84 }
85
86 #pragma mark - TextField
87 - (BOOL)textFieldShouldClear:(UITextField *)textField {
88     NSLog(@"textFieldShouldClear: %@", textField.text); //记录清空按钮触发清空的文本内容;例如:UITextFieldViewModeWhileEditing
89     return YES;
90 }
91
92 @end
时间: 2024-08-10 22:29:48

062在文本输入框中设置一个清空按钮的相关文章

ASP.NET中设置一个定时器来定时更新 转

asp.net 定时器 比较少用,  中国红木网这是一个相当实用的功能,有了RSS博客镜像,就不需要在多处同时发布博客日志了.比如你同时在新浪上有自己的博客,又同时有自己的个人博客站点,那么你只需要在新浪上发布博客日志,而个人博客站点通过RSS博客镜像功能将新浪博客上发布的新日志自动更新到站点中.我们在ASP.NET的,至于asp,由于其天生的局限性,可能无法直接在Web应用程序中完成这一功能,而需要额外的Windows应用程序的支持.1. 管理RSS博客镜像首先我们需要在RSS博客镜像,需要管

37、如何在函数中设置一个全局变量 ?

在函数的内部,通过global声明,使在函数内部中设置一个全局变量,这个全局变量可以在任意的函数中进行调用! SOLR_URL='http://solr.org' def tt(): global SOLR_URL SOLR_URL=SOLR_URL+'#aa' def aa(): if __name__=='__main__': tt() print(SOLR_URL) aa() # http://solr.org#aa 原文地址:https://www.cnblogs.com/zhuifen

101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController<UISearchBarDelegate> 4 @property (strong, nonatomic) UISearchBar *searchBar; 5 @property (strong, nonatomic) NSMutableArray *mArrDataSourceO

为jEasyUi的日期控件添加一个“清空”按钮----通过修改1.4的easyui.min.js

为 jQuery EasyUI 1.4 的datebox或datetimebox添加一个清空按钮 使用场景:为用户指定了日期的格式,且日期可以为空 修改语言包easyui-lang-zh_CN.js 在if ($.fn.datebox){ 的下一行添加 (41或42行) $.fn.datebox.defaults.cleanText = '清空'; 在if ($.fn.datetimebox && $.fn.datebox){  $.extend($.fn.datetimebox.defa

给EasyUI的DateBox控件增加一个清空按钮

 EasyUI中的DateBox控件居然没有清空按钮,如下图: 真是不可思议,对于要求日期格式必须选择的情况下,不能清空日期,非常不方便. 虽然可以通过手工修改EasyUI及相关库文件来实现增加清空按钮功能,但这要求修改EasyUI原生库文件,而且要修改多个地方,势必会造成其它的不兼容问题. 但随着1.3.5版本的发布,这个问题可以很好地解决了,而且不需要修改任何库文件,只需要在页面中日期控件中增加一段代码即可.这是因为1.3.5版本的EasyUI增加了一个属性:buttons,这个属性可以

如何在网页中设置一个定时器计算时间?

写一个简单的计数js代码 首先先写一个div,用于存放计数器,并且通过改变样式可以改变计数器的样子 <body> <div id="le"></div> </body> <style type="text/css"> #le{ position: fixed; right: 100px; top: 200px; width: 250px; height: 200px; background-color: #

Python3 Tkinter基础 Text Button 文本框中插入一个按钮

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: from tkinter import * root=Tk() #30个字符宽 2行 mytext=Text(root,width=30,height=2) mytext.pack(padx=10,pady=10) b1=Button(mytext,text='

在网站中设置一个分享

经常在网页中会看到分享到这个按钮,例如分享到微信 qq 微信朋友圈之类的 现在这块有一个插件可以直接实现这个功能,而且代码简单 地址是https://github.com/zyxhaha/frame/tree/master/calledT-sosh-6503e4 这个插件http://www.calledt.com/soshm/稍微看一下介绍,就可以轻松的实现分享这个功能

Android Studio中设置一个按钮的不同点击触发事件

my_day_model = (RelativeLayout) v.findViewById(R.id.my_day_model);my_day_pic = (ImageView) v.findViewById(R.id.my_day_pic);my_day_text = (TextView) v.findViewById(R.id.my_day_text);my_day_model.setOnClickListener(new View.OnClickListener() { @Overrid