zoukankan      html  css  js  c++  java
  • 自定义TextField清除按钮

    当需要设置TextField的清除按钮的时候,系统的总是不满足需求,这就需要我们自定义了,代码如下:

     1 //
     2 //  TextFieldDemoViewController.m
     3 //  OCDemo
     4 //
     5 //  Created by 思 彭 on 16/12/14.
     6 //  Copyright © 2016年 思 彭. All rights reserved.
     7 //
     8 
     9 #import "TextFieldDemoViewController.h"
    10 
    11 @interface TextFieldDemoViewController ()<UITextFieldDelegate>
    12 
    13 @property (nonatomic, strong) UITextField *textField;
    14 
    15 @end
    16 
    17 @implementation TextFieldDemoViewController
    18 
    19 - (void)viewDidLoad {
    20     [super viewDidLoad];
    21 
    22     self.textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, 200, 35)];
    23     self.textField.delegate = self;
    24     self.textField.placeholder = @"请输入";
    25     self.textField.borderStyle = UITextBorderStyleLine;
    26     [self.view addSubview:self.textField];
    27     
    28     /*
    29     self.textField.clearButtonMode = UITextFieldViewModeAlways;
    30     // 设置textField清除按钮的背景颜色
    31     [self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    32     // 不能改变背景颜色,需要设置imageView属性
    33     [self.textField setValue:[UIColor redColor] forKeyPath:@"_clearButton.backgroundColor"];
    34 //    [self.textField setValue:[UIImage imageNamed:@"iconfont-shuqian_sel_30x30_@2x"] forKeyPath:@"_clearButton.image"];
    35     
    36      */
    37     
    38     UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    39     clearButton.frame = CGRectMake(self.textField.frame.size.width - 10, 10, 10, 10);
    40     [clearButton addTarget:self action:@selector(clearButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    41     [clearButton setImage:[UIImage imageNamed:@"iconfont-shuqian_sel_30x30_@2x"] forState:UIControlStateNormal];
    42     [self.textField addSubview:clearButton];
    43 }
    44 
    45 - (void)clearButtonDidClick: (UIButton *)button {
    46     
    47     self.textField.text = nil;
    48 }
    49 
    50 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    51     
    52     [self.textField resignFirstResponder];
    53     return YES;
    54 }
    55 @end
  • 相关阅读:
    【jQuery Case Study 1】Site:cssninjas.com
    PostgreSQL HA双机热备份配置
    PogreSQL物理备份与逻辑备份
    转:truncate,delete和drop删除表的异同
    pgsequence序列的用例
    Linux下端口占用的解决方法
    Linux下脚本的编写和执行
    schema模式的选用
    pg数据库基础备份的注意事项
    PostgreSQL VMCC多版本并发控制
  • 原文地址:https://www.cnblogs.com/pengsi/p/6178755.html
Copyright © 2011-2022 走看看