首页 > 其他 > 详细

UITextField手动编写

时间:2015-08-06 15:21:07      阅读:203      评论:0      收藏:0      [点我收藏+]

一、UITextField手动编写控件

    UITextField  *txtAccount = [[UITextField allocinitWithFrame:CGRectMake(1010,30030)];

    // 设置委托

    [txtAccount setDelegate:self];

    // 设置占位符

    [txtAccount setPlaceholder:@"账号"];

    // 设置颜色

    [txtAccount setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

    // 设置字体

    [txtAccount setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

    // 设置文本对齐

    [txtAccount setTextAlignment:NSTextAlignmentLeft];

    // 设置样式

    [txtAccount setBorderStyle:UITextBorderStyleRoundedRect];

    // 加入view

    [self.view addSubview: txtAccount];

    [txtAccount release];

二、UITextFieldDelegate委托

// 设置输入框,是否可以被修改

// NO-将无法修改,不出现键盘

// YES-可以修改,默认值 

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

    return YES;

}

// 当点击键盘的返回键(右下角)时,执行该方法。

// 一般用来隐藏键盘

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

    if (txtAccount == textField) {

[txtAccount resignFirstResponder];

}

return YES;

}


// 当输入框获得焦点时,执行该方法。 

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"textFieldDidBeginEditing");


}


// 指定是否允许文本字段结束编辑,允许的话,文本字段会失去first responder 

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

    return YES;

}


// 文本框失去first responder 时,执行  

- (void)textFieldDidEndEditing:(UITextField *)textField{

     NSLog(@"textFieldDidEndEditing");

}

// 指明是否允许根据用户请求清除内容

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

    NSLog(@"textFieldDidEndEditing");

    return YES;

}

// 文本框的文本,是否能被修改 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    return YES;

UITextField手动编写

原文:http://my.oschina.net/u/2427269/blog/488577

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!