UItextField

UItextField相关代码如下:

- (UITextField *)textField
{
    if (_textField == nil) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
        _textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _textField.layer.borderWidth = 0.5;
        _textField.layer.cornerRadius = 3;
        _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
        _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.font = [UIFont systemFontOfSize:15.0];
        _textField.backgroundColor = [UIColor whiteColor];
        _textField.placeholder = @"hello word!";
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.delegate = self;
    }
    
    return _textField;
}

- (void)creatUItextField{
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
    [textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
    
    textField.placeholder = @"默认显示的字"; //默认显示的字
    
    textField.secureTextEntry = YES; //密码
    
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    textField.delegate = self;
    
    UITextField *iptext = [[UITextField alloc]initWithFrame:CGRectMake(45, 435, 300, 45)];
    [iptext setBackgroundColor:[UIColor whiteColor]];
    [iptext setKeyboardType:UIKeyboardTypeNumberPad];
    [iptext setTextAlignment:NSTextAlignmentCenter];
    [iptext setUserInteractionEnabled:NO];
    [iptext setPlaceholder:@"输入正确服务器地址"];
    [iptext setDelegate:self];
    [iptext.layer setBorderColor:[UIColor blackColor].CGColor];
    [iptext.layer setBorderWidth:1];
}

//从其他地方退出键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.textview resignFirstResponder];
}

//当你按下键盘上的retun键时
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.numTF resignFirstResponder];
    return YES;
}
//当行输入文本框将要开始编辑时
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    return  YES;
}
//当输入文本开始进入编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}
//将要完成编辑时
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}

//已经退出模式时
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"%@",textField.text);
}

//键盘弹出与隐藏
- (void)viewWillAppear:(BOOL)animated{

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboarHidd) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboarShow) name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboarHidd{
    
    [UIView animateWithDuration:0.1 animations:^{
        self.loginView.frame = CGRectMake(BOUND_WIDTH/3.5, 100, BOUND_WIDTH/2.3, BOUND_HEIGHT - 120);
    }];
}

- (void)keyboarShow{
    
    [UIView animateWithDuration:0.1 animations:^{
        CGRect frame= self.loginView.frame;
        frame.origin.y -= 160;
        self.loginView.frame = frame;
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //监听软键盘 事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

#pragma mark - keyboard
-(void)WillChangeFrame:(NSNotification *)notif{
    
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    //收起软键盘的时候
    if (value.CGRectValue.origin.y>400) {
        [UIView animateWithDuration:.2 animations:^{
            self.toolbar.frame = CGRectMake(0, self.view.bounds.size.height-45, 320, 45);
        }];
    }
    else
    {
        //弹出软键盘的时候
        [UIView animateWithDuration:.2 animations:^{
            CGRect frame = self.toolbar.frame;
            frame.origin.y = self.view.bounds.size.height -  keyboardSize.height - self.toolbar.bounds.size.height;
            self.toolbar.frame = frame;
        }];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值