一:首先查看一下关于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吗?的更多相关文章

  1. UITextField

    UITextFieldDemo 效果 特点 1.有效定制键盘的样式 2.处理键盘对文本框的遮挡 用法 1.导入文件(UITextField+CreateInputAccessoryView.h/.m) ...

  2. UITextField 基本属性使用

    //设置文本框 透明度 tf.alpha = ; //设置文本颜色 tf.textColor = [UIColor orangeColor]; //设置文本文字 格式 tf.font = [UIFon ...

  3. IOS UITextField &UITextView

    UITextField 限制textField长度 曾经,以为输入框只是输入字符的,但真的认真为一个登陆界面输入框而改了六七次以后,发现好烦人啊,先谢谢测试的不厌其烦,不杀之恩,不想再用IOS的输入框 ...

  4. App你真的需要么

    随着智能手机.移动路联网的普及,APP火的一塌糊涂,APP应用可谓五花八门,街上经常看到各种推广:扫码安装送东西,送优惠券.仿佛一夜之间一个企业没有自己的APP就跟不上时代了. 有时我在想:APP,你 ...

  5. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  6. 你真的会玩SQL吗?之逻辑查询处理阶段

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  7. SQL Server中SELECT会真的阻塞SELECT吗?

    在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...

  8. 您真的理解了SQLSERVER的日志链了吗?

    您真的理解了SQLSERVER的日志链了吗? 先感谢宋沄剑给本人指点迷津,还有郭忠辉童鞋今天在QQ群里抛出的问题 这个问题跟宋沄剑讨论了三天,再次感谢宋沄剑 一直以来,SQLSERVER提供了一个非常 ...

  9. 你真的会玩SQL吗?和平大使 内连接、外连接

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

随机推荐

  1. TCP/IP详解学习笔记(13)-TCP坚持定时器,TCP保活定时器

    TCP一共有四个主要的定时器,前面已经讲到了一个--超时定时器--是TCP里面最复杂的一个,另外的三个是: 坚持定时器 保活定时器 2MSL定时器 其中坚持定时器用于防止通告窗口为0以后双方互相等待死 ...

  2. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要一

    本人有幸在Learning Hard举行的整点抢书活动<Learninghard C#学习笔记>回馈网友,免费送书5本中免费获得了一本<LEARNING HARD C#学习笔记> ...

  3. CSS魔法堂:说说Float那个被埋没的志向

    前言  定位系统中第一难理解就是Normal flow,而第二就非Float莫属了,而Float难理解的原因有俩,1. 一开头我们就用错了:2. 它跟Normal flow靠得太近了.本文尝试理清Fl ...

  4. 基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录

    最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框架保持一致,而在Web上,我主要采用EasyUI的前端界面处理技术,走MVC的技术路线,在重 ...

  5. 从C#到Objective-C,循序渐进学习苹果开发(2)--Objective-C和C#的差异

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台开发苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验. 在上篇<从C#到 ...

  6. 孙鑫MFC学习笔记3:MFC程序运行过程

    1.MFC中WinMain函数的位置在APPMODUL.cpp APPMODUL.cpp中是_tWinMain,其实_tWinMain是一个宏#define _tWinMain WinMain 2.全 ...

  7. int和Integer的区别

    int是一种数据类型,Integer是一个类,有各种方法,如Integer.parseInt(string)等.

  8. Delphi String 常用字串符处理函数

    Delphi 在面对跨平台开发,程序语言也改进不少,不过有些改进,让原本 Delphi 开发者有些不适应,最显注的就是字串处理函数了,原本 Pascal 语言字串起始由 1 开始,几乎是它的经典了,新 ...

  9. 使用JPA储存Text类型的时候 出现乱码的问题

    以前遇到这类问题第一个反应就是觉得客户端和服务端的编码不一样导致的.所以一开始也是那么认为的.以为我们项目使用的是pgsql,默认的就是utf-8,然后我们使用了字符也是utf-8,并且还有一个问题就 ...

  10. springmvc(2)Controller源码简单解析

    前面简单的分析了一下DispatcherServlet,接下来分析一下Controller,在web的MVC中,Controller就是其中的C,启动的一些页面逻辑处理,页面映射的功能: 首先看看超类 ...