ios键盘通知和自定义键盘

一.键盘通知

当文本View(如,,内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘。成为firstresponder可能由用户点击触发,也可向文本View发送消息触发。当文本视图退出first responder时,键盘会消失。文本View退出first responder可能由用户点击键盘上的Done或Return键结束输入触发,也可向文本View发送消息触发。

当键盘显示或消失时,系统会发送相关的通知:

通知消息中的字典中包含键盘的位置和大小信息,对应的key为

UIKeyboardFrameBeginUserInfoKey,UIKeyboardFrameEndUserInfoKey对应的Value是个NSValue对象,内部包含CGRect结构,分别为键盘起始时和终止时的位置信息。

UIKeyboardAnimationCurveUserInfoKey对应的Value是NSNumber对象,内部为类型的数据,表示键盘显示或消失的动画类型。

UIKeyboardAnimationDurationUserInfoKey对应的Value也是NSNumber对象,内部为double类型的数据,表示键盘h显示或消失时动画的持续时间。

例如,在UIKeyboardWillShowNotification,UIKeyboardDidShowNotification通知中的userInfo内容为

userInfo = {UIKeyboardAnimationCurveUserInfoKey = 0;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";UIKeyboardFrameChangedByUserInteraction = 0;UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";}

在UIKeyboardWillHideNotification,UIKeyboardDidHideNotification通知中的userInfo内容为:

userInfo = {UIKeyboardAnimationCurveUserInfoKey = 0;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";UIKeyboardFrameChangedByUserInteraction = 0;UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";}

在Text,Web, and Editing Programming Guide for iOS中,有如下描述Note: The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always {0.0, 0.0}) in rectangle-intersection operations. Because the keyboard is animated into position, the actual bounding rectangle of the keyboard changes over time.但从实际获取的信息来看,矩形的origin并不为{0.0, 0.0},这里应该不准确。

二.文本对象与WebView键盘设置

和都遵循协议,在UITextInputTraits协议中定义了设置键盘的属性,有

1.:键盘类型,如,,,等

2.:键盘Return键显示的文本,默认为”Return”,其他可选择的包括Go,Next,Done,Send,Google等。

3.:键盘外观,默认为,即上图中的浅兰色不透明背景,另外还有一种为,键盘背景为黑色半透明,用于在警告框输入时显示,例如appStore中输入密码时:

若想显示黑色键盘又不想透明露出底部视图,可以将键盘配置成Alert类型的,然后监听键盘显示的广播通知,在显示键盘时在键盘底部增加一不透明黑色背景视图。

注:在苹果的键盘示例程序中,将UITextView键盘类型更改为UIKeyboardAppearanceAlert,在iPad模拟器中运行键盘并没有出现黑色透明的效果,不知为何? 在iPhone中UIKeyboardAppearanceAlert设置有效。

4.:文本大小写样式,见。5.:是否自动更正,见。6.:拼写检查设置,见。7.:是否在无文本时禁用Return键,默认为NO。若为YES,则用户至少输入一个字符后Return键才被激活。8.:若输入的是密码,可设置此类型为YES,输入字符时可显示最后一个字符,其他字符显示为点。

UIWebView本身不直接遵循协议,但同样可设置其内部输入部件的键盘属性。如Configuring the Keyboard for Web Views中所述。

设置autocorrect, auto-capitalization属性。

<input type="text" size="30" autocorrect="off" autocapitalization="on">

设置键盘类型:

Text: <input type="text"></input>Telephone: <input type="tel"></input>URL: <input type="url"></input>Email: <input type="email"></input>Zip code: <input type="text" pattern="[0-9]*"></input>三. 使用键盘通知调整文本视图位置

当文本视图成为First Responser时在窗口底部会显示出键盘,显示的键盘很可能会将文本视图盖住从而无法看到编辑的效果。键盘通知的一大用途即在键盘显示或隐藏时获取到键盘的位置信息,从而可以调整窗口中的文本视图位置或大小,使其可以在键盘上方显示。

Text, Web, and Editing Programming Guide for iOS中的MovingContent That Is Located Under the Keyboard节在键盘显示和消失通知中,通过调整内容视图的和来保证编辑的文本视图不会被键盘盖住。

始终调整好自己观风景的心态,

ios键盘通知和自定义键盘

相关文章:

你感兴趣的文章:

标签云: