zoukankan      html  css  js  c++  java
  • 1月19号 TextField

    @interface ViewController ()

    @property (nonatomic, strong) UITextField *loginTextField;

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        /*

         UITextField定义了一套代理  用来监听空间的状态变化

         */

        

        self.loginTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 320-40, 50)];

        //设置默认提示文本

        self.loginTextField.placeholder = @"QQ/手机号/邮箱";

        //设置textField的边框类型

        self.loginTextField.borderStyle = UITextBorderStyleRoundedRect;

        //设置提示文本居中

        self.loginTextField.textAlignment = NSTextAlignmentCenter;

        

        //设置键盘的类型

        self.loginTextField.keyboardType = UIKeyboardTypeDefault;

        //更改键盘下方的return键的类型

        self.loginTextField.returnKeyType = UIReturnKeySearch;

        

        //设置delegate等于self

        self.loginTextField.delegate = self;

        

        

        [self.view addSubview:_loginTextField];

        

        

        //添加一个清除按钮

        self.loginTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

        

        //设置左边的搜索视图

        UIImage *searchImage = [UIImage imageNamed:@"search"];

        UIImageView *searchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

        searchImageView.image = searchImage;

        

        self.loginTextField.leftView = searchImageView;

        self.loginTextField.leftViewMode = UITextFieldViewModeAlways;

        

        

        //创建右边的视图

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(0, 10, 15, 15);

        [button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];

        button.tag = kChoiceButtonStatusDown;

        [button addTarget:self action:@selector(choiceUser:) forControlEvents:UIControlEventTouchUpInside];

        

        UIView *rView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 30)];

        rView.backgroundColor = [UIColor clearColor];

        [rView addSubview:button];

        

    //    self.loginTextField.rightView = rView;

    //    self.loginTextField.rightViewMode = UITextFieldViewModeAlways;

        

    }

     

    - (void)choiceUser:(UIButton *)sender{

        if (sender.tag == kChoiceButtonStatusDown) {

            [sender setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];

            sender.tag = kChoiceButtonStatusUp;

        } else{

            [sender setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];

            sender.tag = kChoiceButtonStatusDown;

        }

    }

     

    #pragma mark ----UITextFieldDelegate----

    //当键盘的return键被按下了

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{

    //    NSLog(@"here");

        //如何隐藏键盘

        //当点击某个textField,那么这个textField将会作为第一响应者,由于是一个textField,需要用户输入相应地内容,系统会自动弹出一个键盘

        //只需要取消这个textField的第一响应者  那么系统就会隐藏键盘

        [textField resignFirstResponder];

        return YES;

    }

     

     

    //当用户输入过程中  每按下一个字符  那么都会先来调用这个方法

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

        return YES;

    }

     

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        NSLog(@"即将开始编辑");

        return YES;

    }

     

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

        NSLog(@"即将完成编辑");

        return YES;

    }

     

    - (void)textFieldDidBeginEditing:(UITextField *)textField{

        NSLog(@"开始编辑了");

    }

     

    - (void)textFieldDidEndEditing:(UITextField *)textField{

        NSLog(@"编辑结束了");

    }

     

    #pragma mark ----UITouch----

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        //判断textField是不是第一响应者

        if([_loginTextField isFirstResponder]){

            //隐藏键盘

            [_loginTextField resignFirstResponder];

        } else{

            [_loginTextField becomeFirstResponder];

        };

    }

     

     

    @end

  • 相关阅读:
    python入门基础知识
    python数据类型之集合
    python的文件操作
    python 整型,布尔值,字符串相关
    字典和解构初识
    python的小数据池和深浅拷贝
    学习相关的基础知识
    深入理解C指针之一(概念)By kmalloc
    mknod命令及低级文件操作函数
    深入理解C指针之二(数组和指针的关系)By kmalloc
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5142684.html
Copyright © 2011-2022 走看看