iOS(学习3) UITextField 常用属性

#import “ViewController.h"

//设置代理
@interface ViewController ()<UITextFieldDelegate>
{
    UITextField *textField;
    UIButton *_button;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //背景设置成白色
    self.view.backgroundColor = [UIColor yellowColor];
    
    //textField 在视图中的位置
    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 30, 300, 30)];
    //设置边框样式 有圆角 白色背景
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    //背景颜色
    //textField.backgroundColor = [UIColor blueColor];
    
    //提示文字
    textField.placeholder = @"请输入密码";
    
    //设置密文输入
    textField.secureTextEntry = YES;
    
    //设置键盘样式
    textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    //键盘风格  半透明
    /*
     UIKeyboardAppearanceLight  //不透明
     UIKeyboardAppearanceAlert  //半透明
     */
    textField.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    //设置弹出视图 比如自定义键盘
    UIImageView *imageVeiw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"11.jpg"]] ;
    imageVeiw.frame = CGRectMake(100, 10, 320, 220);
    //textField.inputView = imageVeiw;
    
    //左视图  .jpg格式 要带后缀名   png格式不用带后缀名
    textField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"图片名字"]];
    /*
     UITextFieldViewModeUnlessEditing //编辑是消失
     UITextFieldViewModeAlways   //从不消失
     */
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    //清除按钮格式
    textField.clearButtonMode = UITextFieldViewModeAlways;
    //再次编辑时 是否清空
    textField.clearsOnBeginEditing = YES;
    
    //内容对齐方式
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    //文本对齐方式
    textField.textAlignment = NSTextAlignmentCenter;
    
    //滚动
    textField.adjustsFontSizeToFitWidth = NO;
    
    //设置最小字号
    textField.minimumFontSize = 20;
    
    //设置return健样式
    textField.returnKeyType = UIReturnKeyGoogle;

   
    textField.delegate = self;
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
        [self.view addSubview:textField2];
    [self.view addSubview:textField];
    
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(10, 450, 300, 30);
    [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    _button.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_button];
    
    

}

//按钮点击方法
- (void) buttonClick
{
    NSLog(@"haha");
}

- (void)keybarldWillShow
{
    [UIView animateWithDuration:0.20 animations:^{
        _button.frame = CGRectMake(10, 250, 300, 30);
    }];
}

- (void)keybarldWillHhow
{
    [UIView animateWithDuration:0.20 animations:^{
        _button.frame = CGRectMake(10, 450, 300, 30);
    }];
}


//点击屏幕收缩键盘
- (void)controlAction
{
    [textField resignFirstResponder];
}

//文本输入框是否可以进入编辑模式
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"文本输入框是否可以进入编辑模式");
    return YES;
}

//文本输入框已经进入编辑模式
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"文本输入框已经进入编辑模式");
}

//文本输入框是否可以退出编辑模式
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"文本输入框是否可以退出编辑模式");
    return YES;
}

//文本输入框已经退出编辑模式
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"文本输入框已经退出编辑模式");
}

//文本输入框是否点击Retur按钮
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    NSLog(@"文本输入框是否点击Retur按钮");
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值