UITextField 弹出键盘的外观(类型)设置

UITextField 弹出键盘的外观(类型)设置

概要:

UITextField常用的键盘属性如:键盘类型, 安全输入等. 其实这些属性并不是UITextField特有的属性, 而是UITextInputTraits协议属性(一共有8个属性).

文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)

核心API

Class : UITextField

Delegate : UITextInputTraits

涉及的API:

/** 密文输入. */

@property(nonatomic, getter=isSecureTextEntry) BOOL secureTextEntry

/** 键盘类型. */

@property(nonatomic) UIKeyboardType keyboardType

/** 键盘上return按键类型. */

@property(nonatomic) UIReturnKeyType returnKeyType

/** 是否自动显示return按键*/

@property(nonatomic) BOOL enablesReturnKeyAutomatically

/** 键盘显示类型 */

@property(nonatomic) UIKeyboardAppearance keyboardAppearance

/** 设置自动大写模式 */

@property(nonatomic) UITextAutocapitalizationType autocapitalizationType

/** 设置自动纠正类型 */

@property(nonatomic) UITextAutocorrectionType autocorrectionType

/** 设置拼写检查类型 */

@property(nonatomic) UITextSpellCheckingType spellCheckingType

功能实现

Code:

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, 200, 40)];

textField.borderStyle = UITextBorderStyleRoundedRect;

textField.placeholder = @"点击此处输入";

[self.view addSubview:textField];

/** 密文输入*/

textField.secureTextEntry = NO; /**< YES 密文输入. NO 明文输入 */

/** 键盘类型 */

textField.keyboardType = UIKeyboardTypeDefault; /**< 枚举值. */

/** 键盘上return按键类型. */

textField.returnKeyType = UIReturnKeyNext; /**< 枚举值. */

/** 是否自动显示return按键*/

textField.enablesReturnKeyAutomatically = YES; /**< 当用户开始输入时, return按键有效 */

/** 键盘显示类型 */

textField.keyboardAppearance = UIKeyboardAppearanceDark; /**< 枚举值. */

/** 设置自动大写模式 */

textField.autocapitalizationType = UITextAutocapitalizationTypeWords; /**< 枚举值, 每个单词首字母大写 */

/** 设置自动纠正类型 */

textField.autocorrectionType = UITextAutocorrectionTypeYes; /**< 枚举值 */

/** 设置拼写检查类型 */

textField.spellCheckingType = UITextSpellCheckingTypeDefault; /**< 枚举值 */

}

经验分享 程序员 微信小程序 职场和发展