UITextfield属性(初级篇)

来源:互联网 发布:自动生成条码软件 编辑:程序博客网 时间:2024/05/29 17:41

UITextfield用法

创建UITextfield

-(void)createTextField{

UITextField * textFiled = [[UITextField alloc]init];textFiled.tag = 1;textFiled.frame = CGRectMake(20, 100, 280, 40);textFiled.borderStyle = UITextBorderStyleBezel;//边框[self.view addSubview:textFiled];

边框

// 没有边框(默认即为没有边框)

UITextBorderStyleNone

// 线性边框

UITextBorderStyleLine,

// 尖角边框

UITextBorderStyleBezel

圆角边框

UITextBorderStyleRoundedRect

// 圆角时设置背景图不响应

textFiled.background = [UIImage imageNamed:@”button.png”];

// 设置textField为无用状态

textFiled.enabled = NO;

// 设置无用状态的背景图

textFiled.disabledBackground = [UIImage imageNamed:@”player_down_2.png”];

}

键盘

textView.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert,   深灰 石墨色
} UIReturnKeyType;
//设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
UIKeyboardTypeDefault,  默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,  数字键盘
UIKeyboardTypePhonePad,   电话键盘
UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,  数字键盘 有数字和小数点
UIKeyboardTypeTwitter,  优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType

属性

-(void)textSetting{
UITextField * textField = (UITextField *)[self.view viewWithTag:1];
// 设置缺损文字
textField.text = @”我是默认状态”;
// 提示信息
textField.placeholder = @”请输入姓名”;
// 设置文本的字体类型和大小
textField.font = [UIFont italicSystemFontOfSize:30];
// 设置字体颜色(默认为黑色)
textField.textColor = [UIColor redColor];
// 设置对齐方式(默认为居左)
// textField.textAlignment = NSTextAlignmentRight;
// 自适应宽度(可以把字体放小,但是不可以把字体变大)
textField.adjustsFontSizeToFitWidth = YES;
// 自适应的最小字体
textField.minimumFontSize = 15;
// 成为第一响应者
[textField becomeFirstResponder];
// 放弃第一响应者(默认为放弃)
[textField resignFirstResponder];
// BOOL edite =textField.editing;
// NSLog(@”%d”,textField.editing);
textField.clearButtonMode = UITextFieldViewModeAlways;
// 从不显示(默认状态下的)
// UITextFieldViewModeNever,
// 编辑状态下
// UITextFieldViewModeWhileEditing,
// 放弃编辑的时候
// UITextFieldViewModeUnlessEditing,
// 总是显示
// UITextFieldViewModeAlways
UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”player_down_2.png”]];
// leftView的size可以同imageView的size来改变,但是无法改变origin的值,不设置frame,表示默认(居左的textField高度的一个正方形)
UIImageView * imageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”player_down_2.png”]];
// imageView.frame = CGRectMake(100, 100, 3, 100);
textField.leftView = imageView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = imageView1;
textField.rightViewMode = UITextFieldViewModeAlways;
// 注意:设置左右view,不能为同一个view,否则后者会覆盖前者(view不显示,位置空出来)
// 注意2:rightView会覆盖clearButton
// 设置密码效果
// textField.secureTextEntry = YES;
}

限制字数

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

    NSInteger strLength = textField.text.length - range.length + string.length;
    if (range.location >= 4 && strLength >= 4 ) {
    return NO;
    }
    return YES;

}


1 0
原创粉丝点击