iOS 键盘属性及退出几种方法(UITexfield 和 UITextView)

 
UITextView return键退出键盘
#pragma mark - UITextView Delegate Methods   
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
{  
    if ([text isEqualToString:@"\n"]) {  
        [textView resignFirstResponder];  
        return NO;  
    }  
    return YES;  
}  
 
如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。

代码如下:


- (void)textViewDidBeginEditing:(UITextView *)textView {  
   UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];  
   self.navigationItem.rightBarButtonItem = done;      
}  
  
- (void)textViewDidEndEditing:(UITextView *)textView {  
    self.navigationItem.rightBarButtonItem = nil;  
}  
  
- (void)leaveEditMode {  
    [self.textView resignFirstResponder];  
}  

 
 
 
键盘类型     
1. UIKeyboardTypeDefault  默认键盘
2. UIKeyboardTypeASCIICapable   显示ASCII码值得键盘
3. UIKeyboardTypeNumbersAndPunctuation  显示数字和标点符号得键盘
4. UIKeyboardTypeURL  显示带有 .  / .com URL常用得符号得键盘
5. UIKeyboardTypeNumberPad 显示0到9得数字键盘  不支持自动大写
6. UIKeyboardTypePhonePad      显示带有0到9和“*”,“#”得键盘  不支持自动大写
7. UIKeyboardTypeNamePhonePad 显示一个支持输入一个联系人名字或者号码得键盘  不支持自动大写
8. UIKeyboardTypeEmailAddress 显示一个支持输入Email地址符号得键盘 “@”
9. UIKeyboardTypeDecimalPad 显示0到9 和 “."得键盘
10. UIKeyboardTypeAlphabet 显示一个字母键盘

UITextInputTraits Protocol Reference

这个是用来辅助键盘输入得协议,在需要用到键盘输入得地方都需要实现这个协议。UITextField和UITextView已经实现了这个协议。
协议主要包含几个属性,在使用时必须设定。

(1)autocapitalizationType:

确定哪一次shift键被自动按下

UITextAutocapitalizationType:

1.UITextAutocapitalizationTypeNone    不要自动大写任何文本
2. UITextAutocapitalizationTypeWords  自动大写每一个单词得首字母
3. UITextAutocapitalizationTypeSentences  自动大写每一句话得首字母
4. UITextAutocapitalizationTypeAllCharacters  自动大写每一个字母

(2)autocorrectionType

自动纠正提示功能,可以自动提示单词拼写是否正确,并且给出候选正确单词提示。

UITextAutocorrectionType

1.UITextAutocorrectionTypeDefault  自动选择适当得提示给当前得脚本系统
2. UITextAutocorrectionTypeNo  不使用自动纠错
3. UITextAutocorrectionTypeYes  使用自动纠错提示

(3)enablesReturnKeyAutomatically

BOOL类型    设定当文本框没有输入内容时键盘得返回键是否可用

(7)keyboardAppearance

设定键盘显示风格,
1. UIKeyboardAppearanceDefault     默认显示风格
2.  UIKeyboardAppearanceAlert  显示一个合适得弹出式面板
(5)returnKeyType
设定返回键类型
1. UIReturnKeyDefault  设定键盘默认返回键为:“return”
2.  UIReturnKeyGo  设定键盘默认返回键为:“Go”
3. UIReturnKeyGoogle  设定键盘默认返回键为:“Google”
4. UIReturnKeyJoin  设定键盘默认返回键为:“Join”
5.  UIReturnKeyNext  设定为  “Next”
6.  UIReturnKeyRoute  设定为  “Route”
7. UIReturnKeySearch  设定为  “Research”
8. UIReturnKeySend  设定为 “Send”
9. UIReturnKeyYahoo  设定为 “Yahoo”
10. UIReturnKeyDone  设定为 “Done”
11. UIReturnKeyEmergencyCall 设定为 “EmergencyCall”  “紧急电话”

(5)KeybordType

设定键盘类型

(6)secureTextEntry

设定输入文本是否要受到隐藏保护,默认为NO不保护,设定为YES,则文本输入后为密码风格得保护。
要控制键盘是否在输入后消失,我们需要用到 UITextFieldDelegate 
我们这样写,就可以让键盘按下Return键时,让键盘消失。

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return  YES;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值