《iOS 8开发指南》,自己总结用

UITextField

文本框(UITextField)是一种常见的信息输入机制,类似于Web表单中的表单字段。

文本框基础

常用属性

(1) boderStyle属性:设置输入框的边框线样式
(2) backgroundColor属性:设置输入框的背景颜色,使用其font属性设置字体。
(3) clearButtonMode属性:设置一个清空按钮,通过设置clearButtonMode可以指定是否以及何时显示清楚按钮
——》 UITextFieldViewModeAlways 不为空,获得焦点于没有获得焦点都显示清空按钮
——》 UITextFieldViewModeNever 不显示清空按钮
——》UITextFieldViewModeWhileEditing 不为空,且在编辑状态时(及获得焦点)显示清空按钮
——》UITextFieldViewModeUnlessEditing 不为空,且不在编辑状态d时(焦点不在输入框上)显示清空按钮

小例子

-(void)testUITextField{
//    UITextFieldViewModeAlways 不为空,获得焦点于没有获得焦点都显示清空按钮
//    UITextFieldViewModeNever 不显示清空按钮
//    UITextFieldViewModeWhileEditing 不为空,且在编辑状态时(及获得焦点)显示清空按钮
//    UITextFieldViewModeUnlessEditing 不为空,且不在编辑状态d时(焦点不在输入框上)显示清空按钮
    self.view.backgroundColor  = [UIColor whiteColor];
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(20, 100, 280, 30);
    textField.borderStyle = UITextBorderStyleRoundedRect; //圆角
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.placeholder = @"请输入信息";
    [self.view addSubview:textField];
}

设置文本输入框的边框线样式

// 设置文本输入框的边框线样式
-(void)testUITextField2{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITextField* textField1 = [[UITextField alloc] init];
    textField1.delegate = self;
    textField1.frame = CGRectMake(20, 20, 280, 30);
    textField1.borderStyle = UITextBorderStyleLine;
    textField1.text = @"aaaaaaaa";
    textField1.returnKeyType = UIReturnKeyNext;
    [self.view addSubview:textField1];
    
    UITextField *textField2 = [[UITextField alloc]init];
    textField2.delegate = self;
    textField2.frame = CGRectMake(20, 60, 280, 30);
    textField2.borderStyle = UITextBorderStyleBezel;
    textField2.text = @"bbbbbbbbb";
    textField2.returnKeyType = UIReturnKeyNext;
    [self.view addSubview:textField2];
    
    UITextField *textField3 = [[UITextField alloc]init];
    textField3.delegate = self;
    textField3.frame = CGRectMake(20, 100, 280, 30);
    textField3.borderStyle = UITextBorderStyleRoundedRect;
    textField3.text = @"ccccccccc";
    textField3.returnKeyType = UIReturnKeyNext;
    [self.view addSubview:textField3];
    
    UITextField *textField4 = [[UITextField alloc]init];
    textField4.delegate = self;
    textField4.frame = CGRectMake(20, 140, 280, 30);
    textField4.borderStyle = UITextBorderStyleNone;
    textField4.text = @"ddddddddd";
    textField4.returnKeyType = UIReturnKeyNext;
    [self.view addSubview:textField4];
    
    self.textFields_ = [[NSArray alloc]initWithObjects:textField1,textField2,textField3,textField4,nil];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.currentFieldIndex = [self.textFields_ indexOfObject:textField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(self.textFields_.count <= ++self.currentFieldIndex) {
        self.currentFieldIndex = 0;
    }
    // 重新选中UITextField
    UITextField * newField = [self.textFields_ objectAtIndex:self.currentFieldIndex];
    if([newField canBecomeFirstResponder]) {
        [newField becomeFirstResponder];
    }
    return YES;
}

设置文本数据框的字体和颜色

//设置文本数据框的字体和颜色
-(void)testUITextField3{
    self.view.backgroundColor = [UIColor whiteColor];
    UITextField* textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(20, 100, 280, 50);
    textField.borderStyle = UITextBorderStyleBezel;
    textField.backgroundColor = [UIColor blackColor];//设置背景颜色
    textField.textColor = [UIColor redColor];
    textField.font = [UIFont systemFontOfSize:36]; //设置字体大小
    textField.text = @"看我的字体和颜色";
    [self.view addSubview:textField];
}

在文本框中设置一个清空按钮

//在文本框中设置一个清空按钮
-(void)textUITextField4{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITextField *textField1 = [[UITextField alloc]init];
    textField1.delegate = self;
    textField1.clearsOnBeginEditing = YES;
    textField1.frame = CGRectMake(20, 20, 280, 30);
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.clearButtonMode = UITextFieldViewModeNever;
    textField1.text = @"UITextFieldViewModeNever";
    [self.view addSubview:textField1];
    
    UITextField *textField2 = [[UITextField alloc] init];
    textField2.delegate = self;
    textField2.frame = CGRectMake(20, 60, 280, 30);
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField2.text = @"UITextFieldViewModeWhileEditing";
    [self.view addSubview:textField2];
    
    UITextField *textField3 = [[UITextField alloc]init];
    textField3.delegate = self;
    textField3.frame = CGRectMake(20, 100, 280, 30);
    textField3.borderStyle = UITextBorderStyleRoundedRect;
    textField3.clearButtonMode = UITextFieldViewModeUnlessEditing;
    textField3.text = @"UITextFieldViewModeUnlessEditing";
    [self.view addSubview:textField3];
    
    UITextField *textField4 = [[UITextField alloc]init];
    textField4.delegate = self;
    textField4.frame = CGRectMake(20, 140, 280, 30);
    textField4.borderStyle = UITextBorderStyleRoundedRect;
    textField4.clearButtonMode = UITextFieldViewModeAlways;
    textField4.text = @"UITextFieldViewModeAlways";
    [self.view addSubview:textField4];
    
    self.textFields_ = [[NSArray alloc] initWithObjects:textField1,textField2,textField3,textField4, nil];
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    NSLog(@"textFieldShouldClear:%@", textField.text);
    return YES;
}

为文本输入框设置背景图片

// 为文本输入框设置背景图片
-(void)testUITextField5{
    self.view.backgroundColor = [UIColor whiteColor];
    //导入背景图片
    UIImage *imageWithPaper = [UIImage imageNamed:@"按钮背景"];
    UIImage *stretchableWhitePaper = [imageWithPaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    UIImage *imageGrayPaper = [UIImage imageNamed:@"setting_background_four"];
    UIImage *stretchableGrayPaper = [imageGrayPaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    // 创建UITextField实例
    UITextField *textField = [[UITextField alloc]init];
    textField.delegate = self;
    textField.frame = CGRectMake(20, 100, 280, 50);
    textField.background = stretchableWhitePaper;
    textField.disabledBackground = imageGrayPaper;
    textField.text = @"有图片";
    textField.textAlignment = UITextAlignmentCenter;
    textField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:textField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    textField.enabled = NO;
    return YES;
}