UITextField的快速基本使用代码块

 概述

  • UITextField在界面中显示可编辑文本区域的对象。
  • 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入。键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等。文本字段使用目标操作机制和委托对象来报告在编辑过程中所做的更改。
    除了基本的文本编辑行为之外,还可以将叠加视图添加到文本字段以显示其他信息并提供其他可定位控件。您可以为诸如书签按钮或搜索图标等元素添加自定义叠加视图。文本字段提供内置的叠加视图来清除当前文本。自定义覆盖视图的使用是可选的

 

属性和方法

初始化

 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

 

设置占位文本

textField.placeholder = @"请输入文字";

 

设置文本

textField.text = @"测试";

 

设置文本的颜色

textField.textColor = [UIColor redColor];

 

设置文本的字体

textField.font = [UIFont systemFontOfSize:14];

 

设置文本的对齐方式

textField.textAlignment = NSTextAlignmentRight;

 

设置输入框不能编辑

[textField setEnabled:NO];

 

设置编辑框中的内容密码显示

textField.secureTextEntry = YES;

 

启用文本字段时显示的背景图像。该图像显示在文本字段内容的其余部分后面

textField.background = [UIImage imageNamed:@"登录logo"];

 

设置边框样式(更多边框样式到补充说明中查看)默认样式为UITextBorderStyleNone

textField.borderStyle = UITextBorderStyleRoundedRect;

 

设置清楚按钮的模式(更多清楚按钮的模式到补充说明中查看)默认样式为UITextFieldViewModeNever

textField.clearButtonMode = UITextFieldViewModeUnlessEditing;

 

文本字段文本的最小字体大小。当“调整为适合”选项启用时,文本字段会自动更改字体大小以确保文本的最大可读性。你可以使用此属性来指定你认为适合文本的最小字体大小

textField.minimumFontSize = 12;

 

设置键盘类型(更多键盘类型在补充说明中查看)

textField.keyboardType = UIKeyboardTypeNumberPad;

 

设置键盘上返回键的类型(更多返回类型到补充说明中查看)

textField.returnKeyType = UIReturnKeyJoin;

 

设置键盘的视觉样式(更多键盘的视觉样式效果到补充说明中查看)

textField.keyboardAppearance = UIKeyboardAppearanceLight;

 

文本字段的拼写检查行为。此属性决定了拼写检查在打字过程中是启用还是禁用

textField.spellCheckingType = UITextSpellCheckingTypeNo;
此属性决定了拼写检查在打字过程中是启用还是禁用。启用拼写检查后,文本对象会为所有拼写错误的单词生成红色下划线。如果用户点击拼写错误的单词,则文本对象向用户呈现可能的更正列表。
此属性的默认值是default,启用自动更正时启用拼写检查。此属性中的值将覆盖用户在“设置”>“常规”>“键盘”中设置的拼写检查设置。

文本字段的自动纠正行为。此属性确定在输入过程中自动更正是启用还是禁用

textField.autocorrectionType = UITextAutocorrectionTypeYes;

 

自动大写样式适用于键入的文本。此属性决定在什么时候自动按下shift键

textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

 

设置左边试图(注意:需要先设置左边视图的显示模式为UITextFieldViewModeAlways)

textField.leftViewMode = UITextFieldViewModeAlways;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
imageView.image = [UIImage imageNamed:@"验证码"];
textField.leftView = imageView;

 

设置右边视图(注意:需要先设置右变视图的显示模式为UITextFieldViewModeAlways)

textField.rightViewMode = UITextFieldViewModeAlways;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
imageView.image = [UIImage imageNamed:@"验证码"];
textField.rightView = imageView;

 

代理方法

询问代理是否应该在指定的文本字段中开始编辑

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        

// return NO to disallow editing.

 

告诉代理在指定的文本字段中开始编辑

- (void)textFieldDidBeginEditing:(UITextField *)textField;           

// became first responder

 

询问代理是否应在指定的文本字段中停止编辑

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          

// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

 

告诉代理对指定的文本字段停止编辑

- (void)textFieldDidEndEditing:(UITextField *)textField;             

// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

 

告诉代理对指定的文本字段停止编辑

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); 

// if implemented, called in place of textFieldDidEndEditing:

 

询问代理是否应该更改指定的文本

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   

// return NO to not change text

 

询问代理是否应删除文本字段的当前内容

- (BOOL)textFieldShouldClear:(UITextField *)textField;               

// called when clear button pressed. return NO to ignore (no notifications)

 

询问代理文本字段是否应处理按下返回按钮

- (BOOL)textFieldShouldReturn:(UITextField *)textField;              

// called when 'return' key pressed. return NO to ignore.

 

补充说明

设置UITextField占位文字的颜色的两种办法

第一种

KVC修改,如果不先设置占位文字,占位文字的颜色是不管用的:

textField.placeholder = @"占位字符";
textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

第二种

通过attributedPlaceholder属性修改占位文字颜色

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:textField.font
    }];
textField.attributedPlaceholder = attrString;

 

 

 

 

 

 

 

 

自定义键盘(输入视图)

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
redView.backgroundColor = [UIColor redColor];
textField.inputView = redView;
textField.textColor = [UIColor redColor];

 

【转发】https://www.jianshu.com/p/740cd34f870b

posted @ 2019-07-09 06:41  码出境界  阅读(203)  评论(0编辑  收藏  举报