[TwistedFate]UITextField

UITextField

初始化

UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
textFiled.backgroundColor = [UIColor whiteColor];
textFiled.text = @"曹青衣";

对齐方式

// 中央对称
textFiled.textAlignment = NSTextAlignmentCenter;
// 设置字体颜色
textFiled.textColor = [UIColor redColor];

设置字体大小

textFiled.font = [UIFont systemFontOfSize:22];

占位字

textFiled.placeholder = @"请输入xxx";

这里写图片描述

是否允许编辑

// 默认为YES
textFiled.enabled = NO;
// 应用场景:禁言 以及注册完成后的显示界面

是否开始输入的时候清空输入框内容

// 默认NO
textFiled.clearsOnBeginEditing = YES;

是否文字以圆点格式显示(密文输入)

textFiled.secureTextEntry = YES;
// 圆点颜色与字体颜色一样

设置弹出键盘的类型(枚举值)

textFiled.keyboardType = UIKeyboardTypeDefault;

键盘右下角return按钮的类型

textFiled.returnKeyType = UIReturnKeyNext;

自定义键盘(默认是键盘)

// 这个视图能改变的属性只有高度,其余都默认
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 300)];
inputView.backgroundColor = [UIColor greenColor];
textFiled.inputView = inputView;
[inputView release];

输入视图上方的辅助视图(默认为nil)

UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
// 同样只有高度能改变
accessoryView.backgroundColor = [UIColor redColor];
textFiled.inputAccessoryView = accessoryView;
accessoryView release];     // 注意记得释放内存

这里写图片描述

边框样式(枚举值)

textFiled.borderStyle = UITextBorderStyleRoundedRect;

清除按钮模式(默认永不出现)

textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;

输入框左视图 (宽高有影响)

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
leftView.backgroundColor = [UIColor blackColor];
textFiled.leftView = leftView;
[leftView release];

左视图的显示模式

textFiled.leftViewMode = UITextFieldViewModeWhileEditing;

输入框右视图及显示方式

UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
rightView.backgroundColor = [UIColor grayColor];
textFiled.rightView = rightView;
[rightView release];
 // 输入框右视图 显示模式
textFiled.rightViewMode = UITextFieldViewModeWhileEditing;

这里写图片描述

设置代理

// 需要先加入遵守的协议<UITextFieldDelegate>
// 设置代理
textField.delegate = self;

// 显示视图
[self.window addSubview:textFiled];
// 释放空间
[textFiled release];

键盘回收

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 参数是点击的textField
    // 即哪个textField被点击了,作为参数传进这个方法
//    textField.backgroundColor = [UIColor magentaColor];
    // 键盘回收
    // 取消第一响应者 (UIRespond类)
    [textField resignFirstResponder]; 
    // 失去第一响应
//    NSLog(@"触发代理方法了");
    return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值