Textfield的属性设定和键盘回收

7 篇文章 0 订阅
3 篇文章 0 订阅


#import "ViewController.h"


@interface ViewController ()<UITextFieldDelegate>


@property (nonatomic,strong) UITextField *textfield;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //初始化

    UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(50,100, 200, 43)];

    [self.view addSubview: textfield];


    //占位符

    textfield.placeholder = @"请输入";

    

    //设置text的边框样式(圆角)

    textfield.borderStyle = UITextBorderStyleRoundedRect;

    

    //Textfield 显示字体的颜色

    textfield.textColor = [UIColor redColor];

    

    //占位符设置

    textfield.tintColor = [UIColor greenColor];

    

    //设置Textfield左侧view的显示模式

    textfield.leftViewMode = UITextFieldViewModeAlways;

    

    //Textfield左侧添加图片

    UIImageView *leftImgV =  [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];

    leftImgV.image = [UIImage imageNamed:@"notlook.png"];

    textfield.leftView = leftImgV;


    //文字显示的方式默认左侧对齐

    textfield.textAlignment = NSTextAlignmentCenter;

    

    //密文输入

    textfield.secureTextEntry = YES;

    

    //再次输入情况Textfield

    textfield.clearsOnBeginEditing = YES;

    

    //键盘样式

    textfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

    

    //代理方法

    textfield.delegate = self;

    

    //扩充textfield的生命周期

    self.textfield = textfield;


}


#pragma mark ----Textfield代理方法----

//回收键盘

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


    [textField resignFirstResponder];

    return YES;


}

//点击空白处回收键盘

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


    [self.textfield resignFirstResponder];


}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值