文本框->UITextField 类的使用
2015-12-29 15:22
525 查看
UITextField类的使用【部分】
大部分功能可以在playground中演示,
UITextField是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等。
初始化一个文字框:(以下都是OB版本,可按照Swift语言重构类似使用)
UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 100)];
如Swift版本:
var textField = UITextField(frame :CGRect(x:10,y:10,width:150,height:50)
设置和获取文字框文字:
@property(nonatomic,copy)NSString *text;
如Swift版本:
var textField.text = “文本框”
通过AttributedString创建和获取文字:
@property(nonatomic,copy)NSAttributedString *attributedText;
设置字体颜色属性:
@property(nonatomic,retain)UIColor *textColor;
设置字体属性:
@property(nonatomic,retain)UIFont *font;
设置字体对齐格式:
@property(nonatomic)NSTextAlignmenttextAlignment;
设置输入框风格:
@property(nonatomic)UITextBorderStyle borderStyle;
这个风格是一个枚举,如下:
typedefNS_ENUM(NSInteger, UITextBorderStyle) {
//没有任何边框
UITextBorderStyleNone,
//线性边框
UITextBorderStyleLine,
//阴影效果边框
UITextBorderStyleBezel,
//原型效果边框
UITextBorderStyleRoundedRect
};
设置默认字体属性
@property(nonatomic,copy)NSDictionary *defaultTextAttributes;
这个属性的设置会影响到全部字体的属性。
设置缺省时显示的灰度字符串
@property(nonatomic,copy)NSString *placeholder;
通过AttributedString设置缺省字符串
@property(nonatomic,copy)NSAttributedString *attributedPlaceholder;
设置是否在开始编辑时清空输入框内容
@property(nonatomic)BOOL clearsOnBeginEditing;
设置字体大小是否随宽度自适应(默认为NO)
@property(nonatomic)BOOL adjustsFontSizeToFitWidth;
设置最小字体大小
@property(nonatomic)CGFloat minimumFontSize;
设置背景图片(会被拉伸)
@property(nonatomic,retain)UIImage *background;
设置禁用时的背景图片
@property(nonatomic,retain)UIImage *disabledBackground;
是否正在编辑(只读属性)
@property(nonatomic,readonly,getter=isEditing)BOOL editing;
是否允许更改字符属性字典
@property(nonatomic)BOOL allowsEditingTextAttributes;
设置属性字典
@property(nonatomic,copy)NSDictionary *typingAttributes;
设置清除按钮的显示模式
@property(nonatomic)UITextFieldViewMode clearButtonMode;
这是一个枚举,如下:
typedefNS_ENUM(NSInteger, UITextFieldViewMode) {
//从不显示
UITextFieldViewModeNever,
//编辑的时候显示
UITextFieldViewModeWhileEditing,
//非编辑的时候显示
UITextFieldViewModeUnlessEditing,
//任何时候都显示
UITextFieldViewModeAlways
};
设置输入框左边的view
@property(nonatomic,retain)UIView *leftView;
设置输入框左视图的显示模式
@property(nonatomic)UITextFieldViewMode leftViewMode;
设置输入框右边的view
@property(nonatomic,retain)UIView *rightView;
设置输入框右视图的显示模式
@property(nonatomic)UITextFieldViewMode rightViewMode;
设置输入框成为第一响应时弹出的视图和辅助视图(类似键盘)
@property(readwrite, retain) UIView *inputView;
@property(readwrite, retain) UIView *inputAccessoryView;
这个属性设置是否允许再次编辑时在内容中间插入内容
@property(nonatomic)BOOL clearsOnInsertion;
注销第一响应(収键盘)
-(BOOL)endEditing:(BOOL)force;
UITextFieldDelegate代理中的方法
点击输入框时触发的方法,返回YES则可以进入编辑状态,NO则不能。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
开始编辑时调用的方法
-(void)textFieldDidBeginEditing:(UITextField *)textField;
将要结束编辑时调用的方法,返回YES则可以结束编辑状态,NO则不能
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField;
结束编辑调用的方法
-(void)textFieldDidEndEditing:(UITextField *)textField;
输入字符时调用的方法
-(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;
点击清除按钮时调用的函数,返回YES则可以清除,点击NO则不能清除
-(BOOL)textFieldShouldClear:(UITextField *)textField;
点击return键触发的函数
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
部分代码在playground中演示如下:
大部分功能可以在playground中演示,
UITextField是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等。
初始化一个文字框:(以下都是OB版本,可按照Swift语言重构类似使用)
UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 100)];
如Swift版本:
var textField = UITextField(frame :CGRect(x:10,y:10,width:150,height:50)
设置和获取文字框文字:
@property(nonatomic,copy)NSString *text;
如Swift版本:
var textField.text = “文本框”
通过AttributedString创建和获取文字:
@property(nonatomic,copy)NSAttributedString *attributedText;
设置字体颜色属性:
@property(nonatomic,retain)UIColor *textColor;
设置字体属性:
@property(nonatomic,retain)UIFont *font;
设置字体对齐格式:
@property(nonatomic)NSTextAlignmenttextAlignment;
设置输入框风格:
@property(nonatomic)UITextBorderStyle borderStyle;
这个风格是一个枚举,如下:
typedefNS_ENUM(NSInteger, UITextBorderStyle) {
//没有任何边框
UITextBorderStyleNone,
//线性边框
UITextBorderStyleLine,
//阴影效果边框
UITextBorderStyleBezel,
//原型效果边框
UITextBorderStyleRoundedRect
};
设置默认字体属性
@property(nonatomic,copy)NSDictionary *defaultTextAttributes;
这个属性的设置会影响到全部字体的属性。
设置缺省时显示的灰度字符串
@property(nonatomic,copy)NSString *placeholder;
通过AttributedString设置缺省字符串
@property(nonatomic,copy)NSAttributedString *attributedPlaceholder;
设置是否在开始编辑时清空输入框内容
@property(nonatomic)BOOL clearsOnBeginEditing;
设置字体大小是否随宽度自适应(默认为NO)
@property(nonatomic)BOOL adjustsFontSizeToFitWidth;
设置最小字体大小
@property(nonatomic)CGFloat minimumFontSize;
设置背景图片(会被拉伸)
@property(nonatomic,retain)UIImage *background;
设置禁用时的背景图片
@property(nonatomic,retain)UIImage *disabledBackground;
是否正在编辑(只读属性)
@property(nonatomic,readonly,getter=isEditing)BOOL editing;
是否允许更改字符属性字典
@property(nonatomic)BOOL allowsEditingTextAttributes;
设置属性字典
@property(nonatomic,copy)NSDictionary *typingAttributes;
设置清除按钮的显示模式
@property(nonatomic)UITextFieldViewMode clearButtonMode;
这是一个枚举,如下:
typedefNS_ENUM(NSInteger, UITextFieldViewMode) {
//从不显示
UITextFieldViewModeNever,
//编辑的时候显示
UITextFieldViewModeWhileEditing,
//非编辑的时候显示
UITextFieldViewModeUnlessEditing,
//任何时候都显示
UITextFieldViewModeAlways
};
设置输入框左边的view
@property(nonatomic,retain)UIView *leftView;
设置输入框左视图的显示模式
@property(nonatomic)UITextFieldViewMode leftViewMode;
设置输入框右边的view
@property(nonatomic,retain)UIView *rightView;
设置输入框右视图的显示模式
@property(nonatomic)UITextFieldViewMode rightViewMode;
设置输入框成为第一响应时弹出的视图和辅助视图(类似键盘)
@property(readwrite, retain) UIView *inputView;
@property(readwrite, retain) UIView *inputAccessoryView;
这个属性设置是否允许再次编辑时在内容中间插入内容
@property(nonatomic)BOOL clearsOnInsertion;
注销第一响应(収键盘)
-(BOOL)endEditing:(BOOL)force;
UITextFieldDelegate代理中的方法
点击输入框时触发的方法,返回YES则可以进入编辑状态,NO则不能。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
开始编辑时调用的方法
-(void)textFieldDidBeginEditing:(UITextField *)textField;
将要结束编辑时调用的方法,返回YES则可以结束编辑状态,NO则不能
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField;
结束编辑调用的方法
-(void)textFieldDidEndEditing:(UITextField *)textField;
输入字符时调用的方法
-(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;
点击清除按钮时调用的函数,返回YES则可以清除,点击NO则不能清除
-(BOOL)textFieldShouldClear:(UITextField *)textField;
点击return键触发的函数
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
部分代码在playground中演示如下:
相关文章推荐
- 自定义UIButton 图片与label的位置
- Android更新UI的四种方法
- 简化ui文件转换写法
- ios学习笔记UITableViewCell重用时出现重影的解决方案
- 用Fragment制作的Tab页面产生的UI重叠问题
- [友盟微博分享]does not contain bitcode. You must rebuild it with
- 键盘增加监听,UIButton随键盘位移
- iPhone 如何使用UIImageView播放动画,并停留在之后一张图片并添加播放结束时的事件
- HDU1159 Common Subsequence(动态规划)
- UILabel加载HTML文本和字体颜色修改
- Android官网:Configuring Gradle Builds英译
- UIViewController的生命周期及iOS程序执行顺序
- UGUI
- Burp Suite使用介绍——Proxy功能(二)
- oc UIButton绑定事件并传值
- Map集合中value()方法与keySet()、entrySet()区别
- 制作带initrd的uImage
- Burp Suite使用介绍——Proxy功能(一)
- git clone error: The requested URL returned error: 401 Unauthorized while accessing
- Jira plugin cannot startup due to "Error parsing class file"