您的位置:首页 > 产品设计 > UI/UE

键盘(UITextInputTraits)

2015-12-04 13:37 344 查看
1、键盘的种类

UITextView类或者UITextField类都实现了UITextInputTraits协议类,UITextInputTraits协议类中定义了关于键盘的各种属性

CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;

UITextView * textView = [[UITextView alloc] init];
textView.frame = CGRectMake(0, 20, width, 300);
textView.editable = YES;
textView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:textView];
textView.keyboardType = UIKeyboardTypeDefault;

/*
//所有键盘类型
 typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // A number pad with a decimal point.
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // A default keyboard type with URL-oriented addition (shows space . prominently).

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

};
*/(1)、    textView.keyboardType = UIKeyboardTypeDefault;
默认键盘:



(2)、    textView.keyboardType = UIKeyboardTypeASCIICapable;

字母输入用键盘:



(3)、    textView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

数字符号输入用键盘:



(4)、    textView.keyboardType = UIKeyboardTypeURL;

URL输入用键盘:



(5)、    textView.keyboardType = UIKeyboardTypeNumberPad;

数字输入用键盘:



(6)、    textView.keyboardType = UIKeyboardTypePhonePad;

电话号码输入用键盘:



(7)、    textView.keyboardType = UIKeyboardTypeNamePhonePad;

人名和电话号码输入键盘



(8)、    textView.keyboardType = UIKeyboardTypeEmailAddress;

邮件地址输入用键盘:



(9)、    textView.keyboardType = UIKeyboardTypeDecimalPad;

可以输入小数点的数字键盘:



(10)、    textView.keyboardType = UIKeyboardTypeTwitter;

// A type optimized for twitter text entry (easy access to @ #)
应该就是说容易输入@和#的键盘:


(11)、    textView.keyboardType = UIKeyboardTypeWebSearch;



2、键盘背景:

typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, // Default apperance for the current input method.
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated
};

(1)、    textView.keyboardAppearance = UIKeyboardAppearanceDefault;

默认:



(2)、    textView.keyboardAppearance = UIKeyboardAppearanceDark;

深色背景:



(3)、    textView.keyboardAppearance = UIKeyboardAppearanceLight;

浅色背景:



(4)、    textView.keyboardAppearance = UIKeyboardAppearanceAlert;

UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated


类似警告用的黑色背景:



3、return键的类型:

只显示外观,具体按钮功能还需自己实现。

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};

(1)、    textView.returnKeyType = UIReturnKeyDefault;


(2)、    textView.returnKeyType = UIReturnKeyGo;



(3)、    textView.returnKeyType = UIReturnKeyGoogle;



(4)、    textView.returnKeyType = UIReturnKeyJoin;



(5)、    textView.returnKeyType = UIReturnKeyNext;



(6)、    textView.returnKeyType = UIReturnKeyRoute;



(7)、    textView.returnKeyType = UIReturnKeySearch;



(8)、    textView.returnKeyType = UIReturnKeySend;



(9)、    textView.returnKeyType = UIReturnKeyYahoo;



(10)、    textView.returnKeyType = UIReturnKeyDone;



(11)、    textView.returnKeyType = UIReturnKeyEmergencyCall;



(12)、    textView.returnKeyType = UIReturnKeyContinue;



4、return键自动无效:

UITextView * textView = [[UITextView alloc] init];
textView.frame = CGRectMake(0, 20, width, 300);
textView.editable = YES;
textView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:textView];
textView.keyboardType = UIKeyboardTypeDefault;
textView.keyboardAppearance = UIKeyboardAppearanceDefault;
textView.returnKeyType = UIReturnKeyDone;
textView.enablesReturnKeyAutomatically = YES;//设置为YES,在文本为空的情况下return键为灰色无效,不为空的时候为蓝色有效



5、shift键

设置textView的autocapitalizationType属性可以控制shift键是否有效:

(1)、    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;

shift不能自动有效

(2)、    textView.autocapitalizationType = UITextAutocapitalizationTypeWords;

单词开头的情况下自动有效,就是每输入一个单词在开头的时候自动大写,第二个字母开始就自动小写

(3)、    textView.autocapitalizationType = UITextAutocapitalizationTypeSentences;

文章开头有效,可以结合第二个理解

(4)、    textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

一直有效(就是一直大写)

6、自动矫正功能:

typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
};

需设置textView的autocorrectionType属性,UITextAutocorrectionTypeYes开启自动矫正功能,UITextAutocorrectionTypeNo关闭自动矫正功能
7、密码输入:

UITextField * fieldView = [[UITextField alloc] init];
    fieldView.frame = CGRectMake(0, 20, width, 300);
    fieldView.backgroundColor = [UIColor orangeColor];
    fieldView.secureTextEntry = YES;
    [self.view addSubview:fieldView];
其中UITextView也有这个secureTextEntry属性,但即便UITextView的这个属性设置为了YES,也不会以不可见的点形式输入

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息