一:首先查看一下关于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. 基于HTML5快速搭建3D机房设备面板

    以真实设备为模型,搭建出设备面板,并实时获取设备运行参数,显示在设备面板上,这相比于纯数值的设备监控系统显得更加生动直观.今天我们就在HT for Web的3D技术上完成设备面板的搭建. 我们今天模拟 ...

  2. Android Studio快捷键每日一练(6)

    原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-6/ 51.重构代码 苹果:Ctrl+T    Win ...

  3. React Native版本升级的正确姿势

    基于React Native(简称:RN)的APP也发布了三个版本了,RN由于两周就会发布一版从最开始项目用的0.29到最近的0.37,做为一个开源项目来说更新真是跟坐火箭般快速,当然对于我们使用的人 ...

  4. java继承的对象中构造函数的调用顺序

    建立两个继承关系的对象 public class Machine { public String machieNameString; public Machine() { System.out.pri ...

  5. Redis使用总结

    1.Redis安装 redis的安装非常的简单,而且Redis并不依赖其他环境和标准库,很容易上手,这可能也是它流行的一个原因.这里为了测试方便,用的都是windows 环境下测试.下载Windows ...

  6. MEF入门之不求甚解,但力求简单能讲明白(四)

    上一篇我们已经可以获取各种FileHandler的实例和对应的元数据.本篇,我们做一个稍微完整的文件管理器. 1.修改接口IFileHandler,传入文件名 namespace IPart { pu ...

  7. Service随系统启动运行

    Android系统启动时,会发出android.intent.action.BOOT_COMPLETED广播,定义一个类继承自BroadcastReceiver,监听该广播,并在收到该广播时启动Ser ...

  8. 示例 Edit 关闭键盘再显示

    在某一些 Android 的机子上,点入 Edit 显示会键盘,但关闭键盘再点一次 Edit 后,键盘并不会再次显示出来. 实机测试: Sony Xperia ST17i:无法再次显示. Nexus ...

  9. [moka同学笔记]Yii2 数据操作Query Builder

    Query Builder [php] view plain copy   $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name ...

  10. Java的对象初始化过程

    成员变量(字段)初始化顺序 在一个类里初始化的顺序是由成员变量在类里面的定义的顺序来决定的.即使成员变量大量散布于类的各个方法定义的中间,那些成员变量仍会在调用任何方法之前得以初始化,甚至在构造函数调 ...