UITextField详解

UILabel用于提示用户操作,它负责显示文本的信息

UITextField和UIView可用于接收用户输入,前者通常显示一行,后者可以输入很多数据;二者用法相似。

这里主要详细讲解UITextField的用法

1.UITextField基本属性设置

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTextField];
}

- (void)createTextField{
    //1.创建textField并初始化
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(30, 30, 200, 50);
    /*
    等同于(创建的同时初始化)
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 30, 200, 100)];
     */
    
    
    //2.设置边框样式(只有设置了才会有显示边框样式[如果只做了上面的1,显示的是一片空白])
    textField.borderStyle = UITextBorderStyleRoundedRect;//圆角
    /*
    typedef enum{
        UITextBorderStyleNone,//无边框
        UITextBorderStyleLine,//有黑色边框
        UITextBorderStyleBezel,//有灰色边框和阴影
        UITextBorderStyleRoundedRect//圆角
    } UITextBorderStyle
    */
    
    //3.设置边框颜色
    textField.layer.borderColor = [UIColor greenColor].CGColor;
    textField.layer.borderWidth = 1;
    
    
    //4.设置输入框的背景颜色(如果使用了自定义的背景图片边框会被忽略掉
    textField.backgroundColor = [[UIColor alloc]initWithRed:250/255.0 green:240/255.0 blue:230/255.0 alpha:1];
    
    
    
    //5.设置背景(只有在UITextBorderStyleNone样式下,设置背景图才会生效,且图片大小小于textField的frame时,图片才会拉伸)
    textField.background = [UIImage imageNamed:@"1.png"];
    
    //6.默认提示内容(用户开始输入的时候就自动消失)
    textField.placeholder = @"请输入密码";
    
    //7.设置文字的属性
    textField.textColor = [UIColor redColor];//设置输入的文字颜色
    textField.textAlignment = NSTextAlignmentLeft;//居左(此为默认)(Right/Center:右/中间)
    
    
    /*
    8.设置键盘的风格
    textField.keyboardType = UIKeyboardTypeDefault;//默认键盘,支持所有字符
    textField.keyboardType = UIKeyboardTypeNumberPad;//数字键盘
    textField.keyboardType = UIKeyboardTypeDecimalPad;//数字键盘,有数字和小数点
    textField.keyboardType = UIKeyboardTypePhonePad;//电话键盘
    textField.keyboardType = UIKeyboardTypeNamePhonePad;//电话键盘,也支持输入人名
    textField.keyboardType = UIKeyboardTypeEmailAddress;//电子邮件地址键盘
    textField.keyboardType = UIKeyboardTypeTwitter;//优化的键盘,方便输入@、#字符
    
    */
    
    //9.输入框中有个❎ 可设置显示时间
    textField.clearButtonMode = UITextFieldViewModeAlways;
    /*
    typedef enum{
        UITextFieldViewModeNever,         //从不出现
        UITextFieldViewModeWhileEditing,  //编辑时出现
        UITextFieldViewModeUnlessEditing, //除了编辑外都出现
        UITextFieldViewModeAlways         //一直出现
    } UITextFieldViewMode;
    */
    
    //10.安全输入(每输入一个字符就变成一个点)
    textField.secureTextEntry = YES;
    
    [self.view addSubview:textField];
    
}

@end

 

 

转载于:https://www.cnblogs.com/frosting/p/9438436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值