• UITextField


    1、UITextField 的创建

    // 实例化 UITextField 对象
    UITextField *textField = [[UITextField alloc] init];
    // 设置位置尺寸
    textField.frame = CGRectMake(20, 100, 200, 30);
    // 将 textField 加到 window 上显示出来
    [self addSubview:textField];
    

    2、UITextField 的设置

    // 设置背景颜色:默认是透明的
    textField.backgroundColor = [UIColor yellowColor];
    // 设置背景图片
    textField.background = [UIImage imageNamed:@"pic2"];
    // 设置提示文字:用户输入时自动消失
    textField.placeholder = @"请输入用户名";
    // 设置输入的字体颜色
    textField.textColor = [UIColor redColor];
    // 设置文字对齐方式
    textField.textAlignment = NSTextAlignmentLeft;
    // 设置最小可缩小的字号
    textField.minimumFontSize = 10;
    // 自动调整文字大小:自动调整文字的大小以适应 textField 的宽度
    textField.adjustsFontSizeToFitWidth = YES;
    // 设置密文输入模式: default is NO
    textField.secureTextEntry = YES;
    // 设置显示清除按钮
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    // 设置键盘样式
    textField.keyboardType = UIKeyboardTypeDefault;
    // 设置返回键样式
    textField.returnKeyType = UIReturnKeyJoin;
    // 设置输入的字母大小写模式
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    // 设置边框样式
    /*
    UITextBorderStyleNone,                     无边框,默认
    UITextBorderStyleLine,                     直线边框
    UITextBorderStyleBezel,                    边框 + 阴影
    UITextBorderStyleRoundedRect               圆角矩形边框
    */
    textField.borderStyle = UITextBorderStyleLine;
    
    // 设置左右视图显示模式: 不设置模式,左右视图显示不出来
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    // 设置左右视图
    textField.leftView = label1;
    textField.rightView = label2;
    
    // 让 textField 获取第一响应:打开应用程序或界面时直接弹出键盘
    [textField becomeFirstResponder];
    // 让 textField 放弃第一响应:收起键盘
    [textField resignFirstResponder];
    // 设置 textField 的代理,需遵守协议 <UITextFieldDelegate>
    textField.delegate = self;
    

    3、textField 协议方法

    • 协议方法,需遵守协议 UITextFieldDelegate,并设置代理
    // 将要开始编辑,编辑开始前被调用
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        return YES;
    }
    
    // 已经开始编辑,编辑开始后被调用,可监听键盘的弹出
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    }
    
    // 将要结束编辑,编辑结束前被调用
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
        return YES;
    }
    
    // 已经结束编辑,编辑结束后被调用,可监听键盘的回收
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        // 输出 textfield 中输入的内容
        NSLog(@"您输入的内容为:%@", textField.text);
    }
    
    // 是否允许文本修改,文本修改前被调用
    /*
    NO 不允许输入,YES 允许输入(默认)
    range:光标范围
    string:当前输入的内容
    */
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        return YES;
    }
    
    // 返回,键盘上的 return 键触摸后调用
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        return YES;
    }
    
    // 清空,文本输入框中清除按钮被触摸时调用
    - (BOOL)textFieldShouldClear:(UITextField *)textField {
        return YES;
    }
    

    4、textField 的常见需求

  • 相关阅读:
    linux启动init流程(转)
    .bash_profile .bashrc profile 文件的作用的执行顺序(转)
    Linux常用命令
    面试中常见的问题
    systemd启动过程(转)
    .bashrc文件是干什么的(转)
    关于 profile文件(转)
    从MVC框架看MVC架构的设计(转)
    Java高级软件工程师面试考纲(转)
    关于Python中的lambda
  • 原文地址:https://www.cnblogs.com/CH520/p/9413478.html
Copyright © 2020-2023  润新知