iOS——UITextField

本文详细介绍了UITextField的属性和方法,包括文本内容、颜色、字体等基本属性,以及边框样式、背景图片等高级特性,并探讨了UITextField的自定义绘制方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、UITextField 关系图



二、UITextField 属性

1. 保存 UITextField 对象的文本内容;默认为 nil

@property(nullable,nonatomic,copy)  NSString * text; 


2. 保存 UITextField 对象的文本颜色;默认为 nil,使用黑色

@property(nullable,nonatomic,strong)UIColor * textColor;


3. 保存 UITextField 对象的文本字体大小;默认为 nil,使用 12 号

@property(nullable,nonatomic,strong)UIFont * font;


4. 保存 UITextField 对象的文本对齐方式;默认为 NSLeftTextAlignment

@property(nonatomicNSTextAlignment textAlignment;

    // NSTextAlignment 枚举
    typedef NS_ENUM(NSInteger, NSTextAlignment) {
        NSTextAlignmentLeft      = 0,   // 左对齐
        NSTextAlignmentCenter    = 1,   // 右对齐
        NSTextAlignmentRight     = 2,   // 居中
        NSTextAlignmentJustified = 3,   // 在一个段落的最后一行自然对齐
        NSTextAlignmentNatural   = 4,   // 默认的对齐
    }

5. 保存 UITextField 对象的边框样式;默认为 UITextBordeStyleNone

@property(nonatomicUITextBorderStyle borderStyle;

    //UITextFiledStyle 枚举
    typedef NS_ENUM(NSInteger, UITextBorderStyle) {
        UITextBorderStyleNone,       //无边框
        UITextBorderStyleLine,       //黑色线边框
        UITextBorderStyleBezel,      //有边框和阴影
        UITextBorderStyleRoundedRect // 圆角矩形
    };



6. 保存 UITextField 对象的占位符文本;默认为空,且字符是亮灰色

@property(nullable,nonatomic,copyNSString * placeholder;


7. 再次编辑 UITextField 对象是否清空当前的内容;默认为 NO

@property(nonatomicBOOL clearsOnBeginEditing;


8. 设置字体大小是否随边框宽度自适应;默认为 NO

当字体大小过大以至于整个边框不能够全部显示时,会随着 minimumFontSize 的值减少,当已经填满整个边框则不会再减少

@property(nonatomicBOOL adjustsFontSizeToFitWidth;


9. 保存 UITextField 对象的最小字体;默认为 0

@property(nonatomicCGFloat minimumFontSize; 


10. 设置 UITextField 对象的背景图片;默认为空,会拉伸图片

@property(nullable,nonatomic,strong)UIImage * background;


11. 设置 UITextField 对象不可用时的背景图片;默认为空,如果没有设置 background,则忽视

@property(nullable,nonatomic,strong)UIImage * disabledBackground;


12. 保存 UITextField 对象是否处于编辑

@property(nonatomic,readonly,getter=isEditing)BOOL editing;


13. 设置清除按钮的样式,就是文本框右面的叉叉;默认 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode clearButtonMode;

    //UITextFieldViewMode 枚举
    typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
        UITextFieldViewModeNever,         // 从不显示
        UITextFieldViewModeWhileEditing,  //处于编辑模式显示
        UITextFieldViewModeUnlessEditing, //非编辑模式显示
        UITextFieldViewModeAlways         // 总是显示
    };

14. 设置 UITextField 对象的左视图,

@property(nullable,nonatomic,strong)UIView * leftView;


15. 设置 UITextField 对象的左视图的显示模式;默认是 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode leftViewMode; 


16. 设置 UITextField 对象的右视图

@property(nullable,nonatomic,strong)UIView * rightView;


17. 设置 UITextField 对象的右视图的显示模式;默认是 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode rightViewMode;

左视图与右视图中间就是文本区域


18. 当 UITextField 对象称为第一响应者时,弹出的视图(类似于键盘)

@property (nullable,readwrite,strong)UIView *inputView;  


19. 当 UITextField 对象称为第一响应者时,弹出的辅助视图(类似于键盘)       

@property (nullable,readwrite,strong)UIView *inputAccessoryView;


20. 设置是否允许再次编辑时在内容中间插入内容;默认为 NO,即只能子啊头尾插入新的内容

@property(nonatomic)BOOL clearsOnInsertion;


21. UITextField 的委托

@property(nullable,nonatomic,weakid<UITextFieldDelegate> delegate;


三、UITextField方法


1. 重绘方法

除了 UITextField 对象的系统风格外,还可以自定义 UITextField 对象的风格,此时就可以重写如下的方法,这些方法都返回 CGPoint,指定了每个部件的边界范围
①重置边缘区域

- (CGRect)borderRectForBounds:(CGRect)bounds;


②重置文字区域

- (CGRect)textRectForBounds:(CGRect)bounds;


③重置占位符区域

- (CGRect)placeholderRectForBounds:(CGRect)bounds;


④重置编辑区域

- (CGRect)editingRectForBounds:(CGRect)bounds;


⑤重置 clearButton 的区域

- (CGRect)clearButtonRectForBounds:(CGRect)bounds;


⑥重置 leftView 的区域

- (CGRect)leftViewRectForBounds:(CGRect)bounds;


⑦重置 rightView 的区域

- (CGRect)rightViewRectForBounds:(CGRect)bounds;


⑧重写 UITextField 对象的文本的方法

- (void)drawTextInRect:(CGRect)rect;


⑨重写 UITextField 对象的占位符文本的方法

- (void)drawTextInRect:(CGRect)rect;


2. UIView(UITextField)

①取消第一响应者

- (BOOL)endEditing:(BOOL)force;


3. UITextFieldDelegate 协议方法

①点击 UITextField 对象的输入框时触发该方法,返回 YES 则进入编辑模式,否则不进入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;


②获得焦点时触发该方法,成为第一响应者

- (void)textFieldDidBeginEditing:(UITextField *)textField;


③结束编辑时触发该方法,返回 YES 允许停止编辑或注销第一响应者,否则 不允许编辑结束

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;


④失去第一响应者时触发该方法

- (void)textFieldDidEndEditing:(UITextField *)textField;


询问 文本是否可以被替换,如果返回 YES 则可被替换,否则不能被替换

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;


⑦点击清除按钮时触发该方法,返回 YES 可以清除,否则不能清除

- (BOOL)textFieldShouldClear:(UITextField *)textField;


⑧点击返回按钮时触发该方法,返回 YES 可以返回,否则不能返回

- (BOOL)textFieldShouldReturn:(UITextField *)textField;


四、UITextInputTraits 协议

1. 设置键盘的风格样式,默认是UIKeyboardTypeDefault 

@property(nonatomic) UIKeyboardType keyboardType; 

typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                 // 默认键盘样式
    UIKeyboardTypeASCIICapable,            // 输入字母的键盘
    UIKeyboardTypeNumbersAndPunctuation,   //输入数字符号键盘
    UIKeyboardTypeURL,                     //输入 URL 键盘
    UIKeyboardTypeNumberPad,               //输入数字键盘(九键)
    UIKeyboardTypePhonePad,                //输入电话号码键盘
    UIKeyboardTypeNamePhonePad,            //输入人名和电话号码键盘
    UIKeyboardTypeEmailAddress,            //输入邮件地址键盘
    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // 输入数字和小数点的键盘
    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // 容易输入 @ 和 # 的键盘
    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // 搜索网页的键盘
    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,   // 废弃

};

2. 设置键盘的返回键类型;默认是 UIReturnKeyTypeDefault

@property(nonatomic) UIReturnKeyType returnKeyType;

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,        // 显示 return
    UIReturnKeyGo,             // 显示 Go
    UIReturnKeyGoogle,         // 显示 Search
    UIReturnKeyJoin,           // 显示 Join
    UIReturnKeyNext,           // 显示 Next
    UIReturnKeyRoute,          // 显示 Route
    UIReturnKeySearch,         // 显示 Search
    UIReturnKeySend,           // 显示 Send
    UIReturnKeyYahoo,          // 显示 Search
    UIReturnKeyDone,           // 显示 Done
    UIReturnKeyEmergencyCall,  // 显示 EmergencyCall
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),  // 显示 Continue
};

3. 设置是否自动大写

@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;

typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
    UITextAutocapitalizationTypeNone,          // 不能自动有效
    UITextAutocapitalizationTypeWords,         // 单词开头的情况下自动有效,在输入单词时第一个最为大学,剩下的字母为小写
    UITextAutocapitalizationTypeSentences,     // 文章开头的情况下自动有效,在输入很长的文章的时,第一个单词的第一个字母为大写,其余均为小写
    UITextAutocapitalizationTypeAllCharacters, // 一值自动有效,一直是大写
};


4. 设置自动矫正功能;默认是 UITextAutocorrectionTypeDefalut

@property(nonatomic) UITextAutocorrectionType autocorrectionType;

typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
    UITextAutocorrectionTypeDefault,   // 默认
    UITextAutocorrectionTypeNo,        // 禁止自动矫正
    UITextAutocorrectionTypeYes,       // 开启自动矫正
};


5. 设置键盘外观

@property(nonatomic) UIKeyboardAppearance keyboardAppearance;

typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
    UIKeyboardAppearanceDefault,// 默认外观,浅灰色
    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),   // 深灰色
    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),  // 浅灰色
    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // 废弃
};

6. 设置是否安全输入;默认是 NO

@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值