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.
最后,觉得有用记得给个喜欢❤️!非常感谢!