UITextfield属性(初级篇)
2016-03-05 16:36
411 查看
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;
}
相关文章推荐
- UI界面传值
- Customize UIView &UITextField
- iOS自学笔记之UISwitch和UITextField
- UITextField使用代理获取已输入的内容
- 修改TextField的Placeholder属性的字体大小,颜色
- NSMutableAttributedString文本样式设置以及富文本展示
- UIScrollView内容偏移的问题
- Objective-C: UIKit:UITextFiled、UIAlertController(警告框、操作表)、
- OC-XIB使用&UITextField
- OC-UITextField
- XIB中设置UITextField的高度
- iOS UITextField设置金额(钱数)输入框
- 由SequenceFile.Writer(key,value)谈toString()方法
- UESTC--1251--谕神的密码(贪心)
- UESTC--1251--谕神的密码(贪心)
- 需要优化软件UI,so 沉寂许久之后开始学习界面的课程,把文档直接贴过来自己试着翻译
- 《腾讯网UED体验设计之旅》读后感
- self表示的是当前类开辟内存的对象(文档里写的是当前的消息接受者 Return Value: The receiver.)
- hdoj-1058(优先队列)---priority_queue====丑数
- HDU 4126 Genghis Khan the Conqueror(最小生成树)