美文网首页
OC - UITextField设置和键盘

OC - UITextField设置和键盘

作者: 千山小畻 | 来源:发表于2016-06-28 16:30 被阅读2388次

    导读:

    • 一、创建UITextField
    • 二、设置UITextField的leftView/rightView
    • 三、UITextField的常用设置
    • 四、键盘回收
    • 五、键盘通知

    一、创建UITextField

    • UITextField 是UIControl的子类,UIControl又是UIView的子类,所以也是一个视图,只不过比UIView多了两个功能:1.文字显示,2.文本编辑。
    UITextField *text = [[UITextField alloc] int];
    text.frame = CGRectMake(50, 50, 180, 50)]; 
    text.backgroundColor = [UIColor yellowColor]; 
    [self.view addSubview:text]; 
    

    二、设置UITextField的leftView/rightView

    UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
    leftView.frame = CGRectMake(0, 0, 55, 20);
    leftView.contentMode = UIViewContentModeCenter;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.leftView = leftView;
    

    三、UITextField的常用设置

    • UITextField不支持文字换行
    • 设置text右侧清除按钮
      self.textField.clearButtonMode = UITextFieldViewModeAlways;
    • 设置text的边框样式(圆角)
      text.borderStyle = UITextBorderStyleRoundedRect;
    • 设置text默认显示文字(但是不作为文本内容的一部分)
      text.placeholder = @"请输入用户名";
    • 设置text文字
      text.text = @"什么破烂”;
    • 设置文本颜色
      text.textColor = [UIColor blackColor];
    • 设置文本的对齐方式
      text.textAlignment = NSTextAlignmentCenter;
    • 设置文字字体
      text.font = [UIFont systemFontOfSize:18];
    • 设置输入框是否可编辑
      text.enabled = YES;
    • 设置当开始编辑时,是否清除框中内容
      text.clearsOnBeginEditing = YES;
    • 设置密码格式(输入框中内容是否以点的形式显示)
      text.secureTextEntry = YES;
    • 设置弹出键盘的样式(数字键盘)
      text.keyboardType = UIKeyboardTypeNumberPad;
    • 键盘右下角显示样式
      text.returnKeyType = UIReturnKeyGo;
    • 设置tag值
      text.tag = 120;

    三、键盘回收

    • 点击右下角或者回车回收键盘
      1. 遵守协议:@interface ViewController ()<UITextFieldDelegate>
      2. 设置代理:text.delegate = self;
      3. 实现协议中的方法:
      - (BOOL)textFieldShouldReturn:(UITextField *)textField {
      [textField resignFirstResponder];
      return YES;
      }
    • 点击空白处回收键盘
      - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
      {
      [self.textField resignFirstResponder];
      }

    四、键盘通知

    • //在view即将显示时添加对系统发出的键盘通知的监听 
      - (void)viewWillAppear:(BOOL)animated
      {
          [super viewWillAppear:animated];
      //为当前控制器注册键盘弹起和关闭通知
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openKeyboard:) name:UIKeyboardWillShowNotification object:nil];
          [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(closeKeyboard:) name:UIKeyboardWillHideNotification object:nil];
      }
      /** 在view即将消失时取消键盘通知的监听 */
      - (void)viewWillDisappear:(BOOL)animated
      {
          [super viewWillDisappear:animated];
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
      }
      /** 键盘弹起事件 */
      - (void)openKeyboard:(NSNotification *)notification
      {
            NSLog(@"%@",notification.userInfo);
      }
      /** 键盘关闭事件 */
      -(void)closeKeyboard:(NSNotification *)notification
      {
            NSLog(@"关闭了键盘");
      }
      /** 点击空白处回收键盘 */
      - (void)touchesBegan:(NSSet<UITouch *> *)touches   withEvent:(UIEvent *)event
      {
            [self.view endEditing:YES];
      }
      /** 销毁通知 */
      - (void)dealloc
      {
            [[NSNotificationCenter defaultCenter] removeObserver:self];
      }

    相关文章

      网友评论

          本文标题:OC - UITextField设置和键盘

          本文链接:https://www.haomeiwen.com/subject/dletjttx.html