iOS UITextField

UITextField:在界面中显示可编辑文本区域的对象

UITextField的属性和方法

1. text:设置文本
@property(nullable, nonatomic,copy)   NSString               *text;  
 // default is nil
2. attributedText:富文本设置文本
@property(nullable, nonatomic,copy)   NSAttributedString     *attributedText NS_AVAILABLE_IOS(6_0);
 // default is nil
3. textColor:设置文本字体的颜色
@property(nullable, nonatomic,strong) UIColor                *textColor;            
// default is nil. use opaque black
4. font:设置文本字体的大小
@property(nullable, nonatomic,strong) UIFont                 *font;                 
// default is nil. use system font 12 pt
5. NSTextAlignment:设置文本的对齐方式
@property(nonatomic)        NSTextAlignment         textAlignment;        
// default is NSLeftTextAlignment
/* Values for NSTextAlignment */
typedef NS_ENUM(NSInteger, NSTextAlignment) {
    NSTextAlignmentLeft      = 0,    // Visually left aligned
#if TARGET_OS_IPHONE && !0
    NSTextAlignmentCenter    = 1,    // Visually centered
    NSTextAlignmentRight     = 2,    // Visually right aligned
#else /* !TARGET_OS_IPHONE */
    NSTextAlignmentRight     = 1,    // Visually right aligned
    NSTextAlignmentCenter    = 2,    // Visually centered
#endif
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);
NSTextAlignment 说明
NSTextAlignmentLeft 左对齐
NSTextAlignmentCenter 居中
NSTextAlignmentRight 右对齐
NSTextAlignmentJustified 最后一行自然对齐
NSTextAlignmentNatural 默认对齐脚本
6. UITextBorderStyle:边框样式
@property(nonatomic)        UITextBorderStyle       borderStyle;          
// default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
    UITextBorderStyleNone,
    UITextBorderStyleLine,
    UITextBorderStyleBezel,
    UITextBorderStyleRoundedRect
};
UITextBorderStyle 说明
UITextBorderStyleNone 默认样式,文本字段不显示边框
UITextBorderStyleLine 显示边框,有线条
UITextBorderStyleBezel 显示边框,有线条(表圈)
UITextBorderStyleRoundedRect 显示圆角边框(常用)
7. defaultTextAttributes:用来设置textfield文本的属性。例如:间距、斜体等。如果设置了defaultTextAttributes,设置的textfield的一些属性会失效
@property(nonatomic,copy)   NSDictionary<NSAttributedStringKey,id> *defaultTextAttributes NS_AVAILABLE_IOS(7_0); 
// applies attributes to the full range of text. Unset attributes act like default values.
8. placeholder:当提示框没有内容时的占位文本
@property(nullable, nonatomic,copy)   NSString               *placeholder;          
// default is nil. string is drawn 70% gray
9. attributedPlaceholder:富文本设置占位文本
@property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder NS_AVAILABLE_IOS(6_0); 
// default is nil
10. clearsOnBeginEditing:再次编辑是否清空
@property(nonatomic)        BOOL                    clearsOnBeginEditing;
 // default is NO which moves cursor to location clicked. if YES, all text cleared
11. adjustsFontSizeToFitWidth:默认是保持原来大小,而让文本滚动。设置为YES时文本会自动缩小以适应文本窗口大小,缩小到设置的minimumFontSize后不再缩小,而让文本滚动
@property(nonatomic)        BOOL                    adjustsFontSizeToFitWidth; 
// default is NO. if YES, text will shrink to minFontSize along baseline
12. minimumFontSize:设置自动缩小显示的最小字体大小
@property(nonatomic)        CGFloat                 minimumFontSize;     
// default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES
13. delegate:代理
@property(nullable, nonatomic,weak)   id<UITextFieldDelegate> delegate;             // default is nil. weak reference
14. background:启用时表示文本字段背景外观的图像
@property(nullable, nonatomic,strong) UIImage                *background;           
// default is nil. draw in border rect. image should be stretchable
15. disabledBackground:禁用时表示文本字段背景外观的图像
@property(nullable, nonatomic,strong) UIImage                *disabledBackground;   
// default is nil. ignored if background not set. image should be stretchable
16. editing:是否允许编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
17. allowsEditingTextAttributes:文本视图是否允许用户编辑样式信息
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); 
// default is NO. allows editing text attributes with style operations and pasting rich text
18. typingAttributes:要应用于用户输入的新文本的属性。选择更改时自动重置
@property(nullable, nonatomic,copy) NSDictionary<NSAttributedStringKey,id> *typingAttributes NS_AVAILABLE_IOS(6_0); 
// automatically resets when the selection changes
19. clearButtonMode:输入框中是否有个叉号按钮(清除按钮),用于一次性删除输入框中的内容,在什么状态显示
@property(nonatomic)        UITextFieldViewMode  clearButtonMode; 
// sets when the clear button shows up. default is UITextFieldViewModeNever
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
    UITextFieldViewModeNever,
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
};
UITextFieldViewMode 说明
UITextFieldViewModeNever 从不出现
UITextFieldViewModeWhileEditing 在文本字段中编辑文本时显示
UITextFieldViewModeUnlessEditing 仅在文本未被编辑时显示
UITextFieldViewModeAlways 如果文本字段包含文本,则始终显示清除按钮
20. leftView:设置左边视图
@property(nullable, nonatomic,strong) UIView              *leftView;        
// e.g. magnifying glass
21. leftViewMode:左视图在什么状态下显示,同19中的枚举状态
@property(nonatomic)        UITextFieldViewMode  leftViewMode;    
// sets when the left view shows up. default is UITextFieldViewModeNever
22. rightView:设置右边视图
@property(nullable, nonatomic,strong) UIView              *rightView;       
// e.g. bookmarks button
23. rightViewMode:右视图在什么状态下显示,同19中的枚举状态
@property(nonatomic)        UITextFieldViewMode  rightViewMode;   
// sets when the right view shows up. default is UITextFieldViewModeNever
24. 绘制和定位覆盖
// 通过重写来绘制边缘区域
- (CGRect)borderRectForBounds:(CGRect)bounds;
// 通过重写来绘制文字区域
- (CGRect)textRectForBounds:(CGRect)bounds;
// 通过重写来绘制占位区域
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
// 通过重写来绘制编辑区域
- (CGRect)editingRectForBounds:(CGRect)bounds;
// 通过重写来绘制clearButton区域(改变size可能导致图片失真)
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
// 通过重写来绘制左视图
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
// 通过重写来绘制右视图
- (CGRect)rightViewRectForBounds:(CGRect)bounds;

// 通过重写来绘制文字属性。重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不需调用super
- (void)drawTextInRect:(CGRect)rect;
// 通过重写来绘制占位属性。重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不需调用super
- (void)drawPlaceholderInRect:(CGRect)rect;
25. inputView:代替标准的系统键盘
@property (nullable, readwrite, strong) UIView *inputView;             
26. inputAccessoryView:编辑时显示在系统键盘或用户自定义的inputView上面的视图
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
27. clearsOnInsertion:插入文本是否替换先前的内容
@property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); 
// defaults to NO. if YES, the selection UI is hidden, and inserting text will replace the contents of the field. changing the selection will automatically set this to NO.

更多

28. enabled:设置输入框是否可以编辑
@property(nonatomic,getter=isEnabled) BOOL enabled;                                  
// default is YES. if NO, ignores touch events and subclasses may draw differently
29. secureTextEntry:设置密码是否明文显示
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;       
// default is NO 
30. keyboardType:设置键盘类型
typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). 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).
    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

};
31. keyboardAppearance:设置键盘的视觉样式(外观)
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
};
32. returnKeyType:设置键盘上返回键的类型
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,
    UIReturnKeyGo,
    UIReturnKeyGoogle,
    UIReturnKeyJoin,
    UIReturnKeyNext,
    UIReturnKeyRoute,
    UIReturnKeySearch,
    UIReturnKeySend,
    UIReturnKeyYahoo,
    UIReturnKeyDone,
    UIReturnKeyEmergencyCall,
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};
33. autocapitalizationType:自动调整大小写
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType; 
// default is UITextAutocapitalizationTypeSentences
typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
    UITextAutocapitalizationTypeNone,
    UITextAutocapitalizationTypeWords,
    UITextAutocapitalizationTypeSentences,
    UITextAutocapitalizationTypeAllCharacters,
};
34. autocorrectionType:自动更正
@property(nonatomic) UITextAutocorrectionType autocorrectionType;         
// default is UITextAutocorrectionTypeDefault
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
    UITextAutocorrectionTypeDefault,
    UITextAutocorrectionTypeNo,
    UITextAutocorrectionTypeYes,
};
35. spellCheckingType:文本对象的拼写检查样式
@property(nonatomic) UITextSpellCheckingType spellCheckingType NS_AVAILABLE_IOS(5_0); 
// default is UITextSpellCheckingTypeDefault;
typedef NS_ENUM(NSInteger, UITextSpellCheckingType) {
    UITextSpellCheckingTypeDefault,
    UITextSpellCheckingTypeNo,
    UITextSpellCheckingTypeYes,
} NS_ENUM_AVAILABLE_IOS(5_0);
36. smartQuotesType:智能引号的配置状态
@property(nonatomic) UITextSmartQuotesType smartQuotesType NS_AVAILABLE_IOS(11_0); 
// default is UITextSmartQuotesTypeDefault;
typedef NS_ENUM(NSInteger, UITextSmartQuotesType) {
    UITextSmartQuotesTypeDefault,
    UITextSmartQuotesTypeNo,
    UITextSmartQuotesTypeYes,
} NS_ENUM_AVAILABLE_IOS(11_0);
37. smartDashesType:智能短划线的配置状态
@property(nonatomic) UITextSmartDashesType smartDashesType NS_AVAILABLE_IOS(11_0); 
// default is UITextSmartDashesTypeDefault;
typedef NS_ENUM(NSInteger, UITextSmartDashesType) {
    UITextSmartDashesTypeDefault,
    UITextSmartDashesTypeNo,
    UITextSmartDashesTypeYes,
} NS_ENUM_AVAILABLE_IOS(11_0);
38. smartInsertDeleteType:智能插入和删除空格字符的配置状态
@property(nonatomic) UITextSmartInsertDeleteType smartInsertDeleteType NS_AVAILABLE_IOS(11_0); 
// default is UITextSmartInsertDeleteTypeDefault;
typedef NS_ENUM(NSInteger, UITextSmartInsertDeleteType) {
    UITextSmartInsertDeleteTypeDefault,
    UITextSmartInsertDeleteTypeNo,
    UITextSmartInsertDeleteTypeYes,
} NS_ENUM_AVAILABLE_IOS(11_0);

39. UITextFieldDelegate

// 询问委托人是否应该在指定的文本字段中开始编辑
- (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.

最后,觉得有用记得给个喜欢❤️!非常感谢!

简书个人主页:https://www.jianshu.com/u/281c41cc90bc
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容