码迷,mamicode.com
首页 > 移动开发 > 详细

iOS_UITextField 基本操作

时间:2015-06-02 23:33:36      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:uitextfield   基本操作   ios   

基本操作

UITextField *userNameTextField = [[UITextField alloc] init];
    userNameTextField.frame = CGRectMake(30, 100, 220, 50);
    [self.window addSubview:userNameTextField];
    [userNameTextField release];

    // 设置样式
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"Enter your name";
    userNameTextField.text = @"OUTLAN";

    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // 设置右边删除按钮出现时间

    UILabel *leftLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    leftLable.text = @"N";
    // 设置左右视图的显示时间
    userNameTextField.leftView = leftLable;
    userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    [leftLable release];


    userNameTextField.enabled = YES; // 设置是否允许输入
    userNameTextField.clearsOnBeginEditing = NO; // 输入时清空
    userNameTextField.secureTextEntry = NO; // 呈现圆点,一般用于输入密码


    userNameTextField.keyboardAppearance = UIKeyboardAppearanceDark; // 控制键盘颜色为黑
    userNameTextField.keyboardType = UIKeyboardTypeEmailAddress; // 设置键盘样式
    userNameTextField.returnKeyType = UIReturnKeySearch; // 设置return按键的样式


    UIView *keyBoard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];

    keyBoard.backgroundColor = [UIColor greenColor];
//    userNameTextField.inputView = keyBoard; // 替换键盘
    [keyBoard release];

    UIView *inputAccessView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
    inputAccessView.backgroundColor = [UIColor yellowColor];
    userNameTextField.inputAccessoryView = inputAccessView; // 辅助条
    [inputAccessView release];

收回键盘

设置代理对象,通常为self

// 设置代理
    textFiled.delegate = self;

当前类遵守协议

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>

实现协议方法

  - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"点击了Return");
    [textField resignFirstResponder]; // 放弃第一响应者 收回键盘
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"begining");
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"ending");
    return YES;
} 

iOS_UITextField 基本操作

标签:uitextfield   基本操作   ios   

原文地址:http://blog.csdn.net/yadong_zhao/article/details/46335787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!