美文网首页iOS UIKit框架学习
iOS-UIKit框架学习—UITextField

iOS-UIKit框架学习—UITextField

作者: Wynter_Wang | 来源:发表于2017-04-14 17:39 被阅读118次

    UITextField对象是一个显示可编辑的文字和动作消息发送到目标对象,当用户按下返回按钮控制。通常可以使用这个类从用户收集少量的文字,并执行一些立即采取行动,如搜索操作,根据该文本。

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding, UIContentSizeCategoryAdjusting>
    
    // 文本
    @property(nullable, nonatomic,copy)   NSString               *text;
    // 富文本
    @property(nullable, nonatomic,copy)   NSAttributedString     *attributedText NS_AVAILABLE_IOS(6_0);
    // 字体颜色
    @property(nullable, nonatomic,strong) UIColor                *textColor;
    // 字体 默认系统12号
    @property(nullable, nonatomic,strong) UIFont                 *font;
    // 对齐方式
    @property(nonatomic)        NSTextAlignment         textAlignment;
    // 文本边缘风格
    @property(nonatomic)        UITextBorderStyle       borderStyle;
    // 默认富文本属性
    @property(nonatomic,copy)   NSDictionary<NSString *, id>           *defaultTextAttributes NS_AVAILABLE_IOS(7_0);
    // 占位符
    @property(nullable, nonatomic,copy)   NSString               *placeholder;
    // 占位符富文本属性
    @property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder NS_AVAILABLE_IOS(6_0);
    // 在文本开始编辑的时候是否移除旧文本
    @property(nonatomic)        BOOL                    clearsOnBeginEditing;
    // 是否根据文本框宽度自动调节字体大小
    @property(nonatomic)        BOOL                    adjustsFontSizeToFitWidth;
    // 允许最小的字号
    @property(nonatomic)        CGFloat                 minimumFontSize;
    // 代理
    @property(nullable, nonatomic,weak)   id<UITextFieldDelegate> delegate;
    // 表示启用时文本字段的背景外观的图像
    @property(nullable, nonatomic,strong) UIImage                *background;
    // 表示禁用时文本字段的背景外观的图像
    @property(nullable, nonatomic,strong) UIImage                *disabledBackground;
    // 文本是否处于编辑状态
    @property(nonatomic,readonly,getter=isEditing) BOOL editing;
    // 是否可以编辑文本中的属性
    @property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0);
    // 用户输入的新文本属性
    @property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0);
    // 清除按钮类型
    @property(nonatomic)        UITextFieldViewMode  clearButtonMode;
    // 左侧view
    @property(nullable, nonatomic,strong) UIView              *leftView;
    // 左侧视图的类型
    @property(nonatomic)        UITextFieldViewMode  leftViewMode;
    // 右侧view
    @property(nullable, nonatomic,strong) UIView              *rightView;
    // 右侧视图的类型
    @property(nonatomic)        UITextFieldViewMode  rightViewMode;
    
    // 返回接收者的CGRect
    - (CGRect)borderRectForBounds:(CGRect)bounds;
    - (CGRect)textRectForBounds:(CGRect)bounds;
    - (CGRect)placeholderRectForBounds:(CGRect)bounds;
    - (CGRect)editingRectForBounds:(CGRect)bounds;
    - (CGRect)clearButtonRectForBounds:(CGRect)bounds;
    - (CGRect)leftViewRectForBounds:(CGRect)bounds;
    - (CGRect)rightViewRectForBounds:(CGRect)bounds;
    
    // 绘制文本
    - (void)drawTextInRect:(CGRect)rect;
    // 绘制占位符文本
    - (void)drawPlaceholderInRect:(CGRect)rect;
    
    // 输入框成为第一响应显示自定义输入视图
    @property (nullable, readwrite, strong) UIView *inputView;
    // 输入框成为第一响应显示自定义附件输入视图
    @property (nullable, readwrite, strong) UIView *inputAccessoryView;
    // 插入文本之前是否替换前一个文本
    @property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0);
    
    @end
    
    
    @interface UIView (UITextField)
    // 是否结束编辑
    - (BOOL)endEditing:(BOOL)force;
    @end
    
    @protocol UITextFieldDelegate <NSObject>
    
    @optional
    
    // 是否可以在指定文本中编辑
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
    // 开始编辑
    - (void)textFieldDidBeginEditing:(UITextField *)textField;
    // 是否可以在指定文本中停止
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
    // 结束编辑
    - (void)textFieldDidEndEditing:(UITextField *)textField;
    //  替换textFieldDidEndEditing:
    - (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0);
    // 指定文本是否可以发生改变
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
    
    // 是否可以清除指定内容
    - (BOOL)textFieldShouldClear:(UITextField *)textField;
    // 按下返回按钮按下是否
    - (BOOL)textFieldShouldReturn:(UITextField *)textField;
    
    @end
    
    
    
    // 边界风格
    typedef NS_ENUM(NSInteger, UITextBorderStyle) {
        UITextBorderStyleNone,        //  无
        UITextBorderStyleLine,        // 矩形外观
        UITextBorderStyleBezel,       // 阴影矩形外观
        UITextBorderStyleRoundedRect  // 圆角矩形
    };
    
    // 右侧清空按钮
    typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
        UITextFieldViewModeNever,         // 从不出现
        UITextFieldViewModeWhileEditing,  // 编辑时出现
        UITextFieldViewModeUnlessEditing, // 除了编辑外出现
        UITextFieldViewModeAlways         // 一直出现
    };
    
    // 将要编辑完成的状态
    typedef NS_ENUM(NSInteger, UITextFieldDidEndEditingReason) {
        UITextFieldDidEndEditingReasonCommitted, // 提交编辑
        UITextFieldDidEndEditingReasonCancelled  // 取消编辑
    } NS_ENUM_AVAILABLE_IOS(10_0);
    
    
    // 开始编辑时发送通知
    UIKIT_EXTERN NSNotificationName const UITextFieldTextDidBeginEditingNotification;
    // 编辑结束时发送通知
    UIKIT_EXTERN NSNotificationName const UITextFieldTextDidEndEditingNotification;
    // 内容变化时发送通知
    UIKIT_EXTERN NSNotificationName const UITextFieldTextDidChangeNotification;
    
    UIKIT_EXTERN NSString *const UITextFieldDidEndEditingReasonKey NS_AVAILABLE_IOS(10_0);
    
    

    e.g.

    UITextField *TF = [[UITextField alloc] init];
    TF.placeholder = @"请输入用户名";
    TF.backgroundColor = [UIColor clearColor];
    TF.delegate = self;
    TF.clearButtonMode = UITextFieldViewModeWhileEditing;
    TF.secureTextEntry = YES;
    TF.font = [UIFont systemFontOfSize:15.0];
    TF.textColor = UIColorFromRGB(0x1a2220);
    TF.tintColor = UIColorFromRGB(0x25cfa6); // 光标颜色
    TF.keyboardType = UIKeyboardTypeNumberPad;
    TF.returnKeyType = UIReturnKeyNext;
    TF.text = @"18888888888";
    [self.view addSubview:TF];
    

    相关文章

      网友评论

        本文标题:iOS-UIKit框架学习—UITextField

        本文链接:https://www.haomeiwen.com/subject/ewqgattx.html