你真的了解UITextField吗?
一:首先查看一下关于UITextField的定义
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding> @property(nullable, nonatomic,copy) NSString *text; //值
@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); //富文本的值
@property(nullable, nonatomic,strong) UIColor *textColor; // 文字色
@property(nullable, nonatomic,strong) UIFont *font; // 字体
@property(nonatomic) NSTextAlignment textAlignment; // 默认值 NSLeftTextAlignment 排版
@property(nonatomic) UITextBorderStyle borderStyle; // 默认值 UITextBorderStyleNone 边框的样式
@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0); // 文本属性 用字典填充 @property(nullable, nonatomic,copy) NSString *placeholder; // 空值时的提示文字
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // 富文本的空值提示文字
@property(nonatomic) BOOL clearsOnBeginEditing; // 默认值 NO 如果设为YES 再次编辑就清空
@property(nonatomic) BOOL adjustsFontSizeToFitWidth; // 默认值NO,若设为YES 当文字超出文本框宽度时,自动调整文字大小
@property(nonatomic) CGFloat minimumFontSize; // 默认值 0.0.最小可缩小的字号和adjustsFontSizeToFitWidth一起使用
@property(nullable, nonatomic,weak) id<UITextFieldDelegate> delegate; //代理
@property(nullable, nonatomic,strong) UIImage *background; //设置背景 注意只有UITextBorderStyleNone的时候改属性有效
@property(nullable, nonatomic,strong) UIImage *disabledBackground; //禁用的背景 @property(nonatomic,readonly,getter=isEditing) BOOL editing; //是否被编辑
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // 默认值 NO
@property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); @property(nonatomic) UITextFieldViewMode clearButtonMode; // 输入框中是否有个叉号 设置清除 何时显示 @property(nullable, nonatomic,strong) UIView *leftView; // 左边视图
@property(nonatomic) UITextFieldViewMode leftViewMode; // 在什么状态下显示 默认值 UITextFieldViewModeNever @property(nullable, nonatomic,strong) UIView *rightView; // 右边视图
@property(nonatomic) UITextFieldViewMode rightViewMode; // 默认值 UITextFieldViewModeNever //界面重写绘制行为
////重写来重置边缘区域
- (CGRect)borderRectForBounds:(CGRect)bounds;
//重写来重置文字区域
- (CGRect)textRectForBounds:(CGRect)bounds;
//重写来重置占位符区域
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
//重写来重置编辑区域
- (CGRect)editingRectForBounds:(CGRect)bounds;
//重写来重置clearButton位置,改变size可能导致button的图片失真
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds; //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
- (void)drawTextInRect:(CGRect)rect;
//重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
- (void)drawPlaceholderInRect:(CGRect)rect; @property (nullable, readwrite, strong) UIView *inputView; //代替标准的系统键盘
@property (nullable, readwrite, strong) UIView *inputAccessoryView; //编辑时显示在系统键盘或用户自定义的inputView上面的视图 @property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); // 默认值NO
@end
UITextField是继承于UIControl,并且遵循NSCoding及UITextInput的协议;UITextInput的协议作用是用来辅助键盘输入得协议,在需要用到键盘输入得地方都需要实现这个协议;
知识点1:UITextBorderStyle的枚举内容
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone,/无任何边框
UITextBorderStyleLine,//黑色线框
UITextBorderStyleBezel,//边线+阴影
UITextBorderStyleRoundedRect //浅色带圆弧线框
};
知识点2:UITextFieldViewMode的枚举内容
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,//无清除按钮
UITextFieldViewModeWhileEditing,//编辑时出现清除按钮
UITextFieldViewModeUnlessEditing,//编辑时不出现,编辑后才出现清除按钮
UITextFieldViewModeAlways //一直显示清除按钮
};
知识点3:关于NSTextAlignment的说明,UITextAlignmentLeft (水平右对齐),UITextAlignmentCenter(水平居中对齐),UITextAlignmentRight (水平右对齐);
知识点4:修改UITextField中关于Placeholder的着色
[self.myTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"]; 当然也可以采用另外一种方式: NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"水印提示文字"];
[attrString addAttribute:NSForegroundColorAttributeNam
value:[UIColor redColor]
range:NSMakeRange(, )];
myTextFieldattributedPlaceholder = attrString;
知识点5:关于leftView跟leftViewMode的运用
_relayTextField = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
_relayTextField.backgroundColor = [UIColor whiteColor];
_relayTextField.font = [UIFont fontWithName:@"OpenSans" size:15.0];
_relayTextField.delegate = self;
_relayTextField.layer.cornerRadius = 5.0f;
_relayTextField.layer.masksToBounds = YES;
_relayTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
_relayTextField.autocorrectionType = UITextAutocorrectionTypeNo;
_relayTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
_relayTextField.returnKeyType=UIReturnKeySend;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
label.font = [UIFont boldSystemFontOfSize:];
label.adjustsFontSizeToFitWidth = YES;
label.textColor = COLOR_WORD_GRAY_2;
_relayTextField.leftView = label;
_relayTextField.leftViewMode = UITextFieldViewModeAlways;
[self addSubview:_relayTextField]; 设置显示的值: -(void)configuerWithName:(NSString*)name messageId:(NSString*)message_id
{
_name = name;
_messageId = message_id;
if ([_relayTextField.leftView isKindOfClass:[UILabel class]]) {
((UILabel*)_relayTextField.leftView).text = [NSString stringWithFormat:@"回复%@ :",name];
((UILabel*)_relayTextField.leftView).textColor = COLOR_WORD_GRAY_2;
}
}
知识点6:inputView跟inputAccessoryView 默认是系统的键盘,比如有自定义的键盘就可以把它赋值给inputView;当然也可以是其它效果的视图,比如弹出选择器等;实例如下:
//自定义键盘,将原有的键盘隐藏,显示当前自定义视图
UIView *keyBoardContent = [[UIView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
keyBoardContent.backgroundColor = [UIColor darkGrayColor];
myTextField.inputView = keyBoardContent; UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
subView.backgroundColor = [UIColor greenColor];
myTextField.inputAccessoryView = subView;
二:UITextInputTraits的相关内容,UITextField遵循这个协议,用来辅助键盘输入得协议,在需要用到键盘输入得地方都需要实现这个协议
@protocol UITextInputTraits <NSObject> @optional @property(nonatomic) UITextAutocapitalizationType autocapitalizationType; // default is UITextAutocapitalizationTypeSentences
@property(nonatomic) UITextAutocorrectionType autocorrectionType; //是否纠错 默认值 UITextAutocorrectionTypeDefault
@property(nonatomic) UITextSpellCheckingType spellCheckingType NS_AVAILABLE_IOS(5_0); // default is UITextSpellCheckingTypeDefault;
@property(nonatomic) UIKeyboardType keyboardType; // 键盘类型 默认值UIKeyboardTypeDefault
@property(nonatomic) UIKeyboardAppearance keyboardAppearance; // 设定键盘显示风格 默认值 UIKeyboardAppearanceDefault
@property(nonatomic) UIReturnKeyType returnKeyType; // default is UIReturnKeyDefault (See note under UIReturnKeyType enum)
@property(nonatomic) BOOL enablesReturnKeyAutomatically; // 设定当文本框没有输入内容时键盘得返回键是否可用@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry; // 设定输入文本是否要受到隐藏保护,默认为NO不保护,设定为YES,则文本输入后为密码风格得保护 @end
知识点7:secureTextEntry是设置是否显示成密码的形式,此属性并不在UITextField里面
知识点8:UIReturnKeyType(UITextInputTraits中)的枚举内容如下
UIReturnKeyDefault ;//默认灰色按钮,标有Return
UIReturnKeyGo ;//标有Go的蓝色按钮
UIReturnKeyGoogle ;//标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin ;//标有Join的蓝色按钮
UIReturnKeyNext ;//标有Next的蓝色按钮
UIReturnKeyRoute ;//标有Route的蓝色按钮
UIReturnKeySearch ;//标有Search的蓝色按钮
UIReturnKeySend ;//标有Send的蓝色按钮
UIReturnKeyYahoo ;//标有Yahoo的蓝色按钮
UIReturnKeyYahoo ;//标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall;//紧急呼叫按钮
知识点9:autocapitalizationType(UITextInputTraits中)首字母是否大写
UITextAutocapitalizationTypeNone ;//不自动大写
UITextAutocapitalizationTypeWords ;//单词首字母大写
UITextAutocapitalizationTypeSentences ;//句子的首字母大写
UITextAutocapitalizationTypeAllCharacters;//所有字母都大写
知识点10:keyboardAppearance(UITextInputTraits中)键盘外观
UIKeyboardAppearanceDefault ;//默认外观,浅灰色
UIKeyboardAppearanceAlert ;//深灰石墨色
知识点11:autocorrectionType(UITextInputTraits中)是否纠错
UITextAutocorrectionTypeDefault ; //默认
UITextAutocorrectionTypeNo ; //不自动纠错
UITextAutocorrectionTypeYes ; //自动纠错
知识点12:使文本框在界面打开时就获取焦点,并弹出输入键盘:[myTextField becomeFirstResponder]; 使文本框失去焦点,并收回键盘[myTextFieldresignfirstresponder]
键盘收起其它方式
//方式 1:
[[[UIApplication sharedApplication]keyWindow] endEditing:YES];
//方式 2:
[[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
//方式 3:
[self.view endEditing:YES];
知识点13:因为UITextFiled是继承于UIControl,所以它也是有增加事件addTaget的效果
[myTextField addTarget:self
action:@selector(textFieldDidChange:)//私有方法
forControlEvents:UIControlEventEditingChanged]; - (void)textFieldDidChange:(UITextField*)textField{//类似UIButton addTag..监听方法
NSLog(@"输入框内容 = %@",textField.text);
}
知识点14:关于UITextFiled的NotificaitonCenter监听
UITextFieldTextDidBeginEditingNotification ;//监听开始编辑
UITextFieldTextDidChangeNotification ;//监听正在编辑
UITextFieldTextDidEndEditingNotification ;//监听结束编辑
实例:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(texfFieldNotification:)
name:UITextFieldTextDidChangeNotification object:nil]; - (void)texfFieldNotification:(NSNotification*)noti{//私有方法
NSLog(@"输入款变化了");
} 当然记得在用后时移除通知;
知识点15:监听键盘出现,知晓键盘CGRect
- (void)efObserverKeyBoardShowAndHidde{
//注册键盘出现
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyBoardWasShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyBoardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyBoardWasShow:(NSNotification*)aNotification{
NSLog(@"键盘出现");
//键盘高度
//CGRect keyBoardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
-(void)keyBoardWillBeHidden:(NSNotification*)aNotification{
NSLog(@"键盘消失");
}
- (void)removeObserver{//移除所有通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
三:UITextField定义的协议UITextFieldDelegate内容
@protocol UITextFieldDelegate <NSObject> @optional - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // 返回一个BOOL值,指定是否循序文本字段开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField; // 开始编辑时触发,获得焦点时
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // 返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束后
- (void)textFieldDidEndEditing:(UITextField *)textField; // 结束编辑 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // 用户不段的输入,可以控制达到某些条件,禁止输入 - (BOOL)textFieldShouldClear:(UITextField *)textField; // 返回一个BOOL值指明是否满足什么条件清除内容
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // 返回一个BOOL值,指明是否允许在按下回车键时结束编辑 @end
知识点1:运用实例
//限定输入条件
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//得到输入框的内容
NSString * textString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if ([string isEqualToString:@"\n"]) {
NSLog(@"回车");
}
if ([string isEqualToString:@""]) {
NSLog(@"后退");
}
if ([string isEqualToString:@" "]) {
NSLog(@"空格");
//return NO;//禁止空格输入
}
if (textString.length>) {
NSLog(@"输入超过5位数");
}
//打印当前键盘输入模式,en-US,zh-Hans....
NSString *lang = [[UITextInputMode currentInputMode]primaryLanguage]; //ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
//限制输入已知的字符
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"]invertedSet];
//按cs分离出数组,数组按@""分离出字符串
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
BOOL canChange = [string isEqualToString:filtered]; return YES;
} - (BOOL)textFieldShouldReturn:(UITextField *)textField{//辞去职务
//主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
[textField resignFirstResponder];
return YES;
}
你真的了解UITextField吗?的更多相关文章
- UITextField
UITextFieldDemo 效果 特点 1.有效定制键盘的样式 2.处理键盘对文本框的遮挡 用法 1.导入文件(UITextField+CreateInputAccessoryView.h/.m) ...
- UITextField 基本属性使用
//设置文本框 透明度 tf.alpha = ; //设置文本颜色 tf.textColor = [UIColor orangeColor]; //设置文本文字 格式 tf.font = [UIFon ...
- IOS UITextField &UITextView
UITextField 限制textField长度 曾经,以为输入框只是输入字符的,但真的认真为一个登陆界面输入框而改了六七次以后,发现好烦人啊,先谢谢测试的不厌其烦,不杀之恩,不想再用IOS的输入框 ...
- App你真的需要么
随着智能手机.移动路联网的普及,APP火的一塌糊涂,APP应用可谓五花八门,街上经常看到各种推广:扫码安装送东西,送优惠券.仿佛一夜之间一个企业没有自己的APP就跟不上时代了. 有时我在想:APP,你 ...
- [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?
你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...
- 你真的会玩SQL吗?之逻辑查询处理阶段
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- SQL Server中SELECT会真的阻塞SELECT吗?
在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...
- 您真的理解了SQLSERVER的日志链了吗?
您真的理解了SQLSERVER的日志链了吗? 先感谢宋沄剑给本人指点迷津,还有郭忠辉童鞋今天在QQ群里抛出的问题 这个问题跟宋沄剑讨论了三天,再次感谢宋沄剑 一直以来,SQLSERVER提供了一个非常 ...
- 你真的会玩SQL吗?和平大使 内连接、外连接
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
随机推荐
- 基于HTML5的WebGL设计汉诺塔3D游戏
在这里我们将构造一个基于HT for Web的HTML5+JavaScript来实现汉诺塔游戏. http://hightopo.com/demo/hanoi_20151106/index.html ...
- [python基础]关于装饰器
在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是 ...
- MVC EF中Attach和Entry区别
EF:加入容器三种方式-->查询,attach,entry.不在容器里,不能用remove,add等方法. 1.0 使用Attach可能会出错.//attach 意思:附上; 贴上,系;var ...
- 从C#到Objective-C,循序渐进学习苹果开发(6)--视图控制器的使用
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...
- 为什么要用 Bootstrap
[Bootstrap](http://hovertree.com/menu/bootstrap/) 是由两个 twitter 员工开发并开源的前端框架,非常火爆,而如此火爆自然有它的道理,在我们团队的 ...
- iOS阶段学习第18天笔记(Plist-Archiver-归档与解归档操作)
iOS学习(OC语言)知识点整理 一.归档与解归档的操作 1)归档是一个过程,将一个或多个对象存储起来,以便以后可以还原,包括将对象存入文件,以后再读取 将数据对象归档成plist文件 2)plist ...
- webstorage[html5的本地数据处理]
1.webStorage是什么? webStorage是html5中用于本地化存储的一种方式,而在之前呢我们是用cookie的存储方式处理; 2.那它们之间的区别是什么? Ⅰ.cookie存在的问题: ...
- 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource
[源码下载] 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource 作者:webabcd 介绍背水一战 Windows 10 之 资源 St ...
- Eclipse web项目引用其它项目时的部署问题
地址:http://blog.csdn.net/testcs_dn/article/details/43764497
- AspectJ对AOP的实现
一:你应该明白的知识 1.对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且 ...