UITextField

 

 

  1 //
  2 //  UITextField.h
  3 //  UIKit
  4 //
  5 //  Copyright (c) 2005-2017 Apple Inc. All rights reserved.
  6 //
  7 
  8 #import <Foundation/Foundation.h>
  9 #import <CoreGraphics/CoreGraphics.h>
 10 #import <UIKit/UIKitDefines.h>
 11 #import <UIKit/UIControl.h>
 12 #import <UIKit/UIFont.h>
 13 #import <UIKit/UIStringDrawing.h>
 14 #import <UIKit/UITextDragging.h>
 15 #import <UIKit/UITextDropping.h>
 16 #import <UIKit/UITextInput.h>
 17 #import <UIKit/UIContentSizeCategoryAdjusting.h>
 18 #import <UIKit/UITextPasteConfigurationSupporting.h>
 19 
 20 NS_ASSUME_NONNULL_BEGIN
 21 
 22 @class UIImage, UIImageView, UILabel, UIColor, UIButton;
 23 @class UITextFieldAtomBackgroundView;
 24 @class UITextFieldBackgroundView;
 25 @class UITextFieldBorderView;
 26 @class UITextFieldLabel;
 27 @class UITextInputTraits;
 28 @class UITextSelectionView;
 29 @class UITextInteractionAssistant;
 30 @class UIPopoverController;
 31 @protocol UITextFieldDelegate;
 32 @protocol UITextSelecting;
 33 //边框样式
 34 typedef NS_ENUM(NSInteger, UITextBorderStyle) {
 35     UITextBorderStyleNone,//无边框
 36     UITextBorderStyleLine,//长方形边框
 37     UITextBorderStyleBezel,//长方形边框
 38     UITextBorderStyleRoundedRect//带圆角的长方形边框
 39 };
 40 //输入框中是否有个×号,在什么时候显示,用于一次性删除输入框中所有的文字,默认不显示
 41 typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
 42     UITextFieldViewModeNever,//永不显示
 43     UITextFieldViewModeWhileEditing,//在编辑状态时显示
 44     UITextFieldViewModeUnlessEditing,//除了编辑状态时都显示
 45     UITextFieldViewModeAlways//一直显示
 46 };
 47 
 48 typedef NS_ENUM(NSInteger, UITextFieldDidEndEditingReason) {
 49     UITextFieldDidEndEditingReasonCommitted,
 50     UITextFieldDidEndEditingReasonCancelled UIKIT_AVAILABLE_TVOS_ONLY(10_0)
 51 } NS_ENUM_AVAILABLE_IOS(10_0);
 52 
 53 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding, UIContentSizeCategoryAdjusting>
 54 
 55 @property(nullable, nonatomic,copy)   NSString               *text;                 // 文字内容 default is nil
 56 @property(nullable, nonatomic,copy)   NSAttributedString     *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
 57 @property(nullable, nonatomic,strong) UIColor                *textColor;            // 字体颜色 default is nil. use opaque black
 58 @property(nullable, nonatomic,strong) UIFont                 *font;                 // 字体样式 default is nil. use system font 12 pt
 59 @property(nonatomic)        NSTextAlignment         textAlignment;        //文字对齐方式默认左对齐  default is NSLeftTextAlignment
 60 @property(nonatomic)        UITextBorderStyle       borderStyle;          //边框样式 default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.
 61 @property(nonatomic,copy)   NSDictionary<NSString *, id>           *defaultTextAttributes NS_AVAILABLE_IOS(7_0); // applies attributes to the full range of text. Unset attributes act like default values.
 62 
 63 @property(nullable, nonatomic,copy)   NSString               *placeholder;          // 当输入框没有内容时,显示的提示文字   default is nil. string is drawn 70% gray
 64 @property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
 65 @property(nonatomic)        BOOL                    clearsOnBeginEditing; // default is NO which moves cursor to location clicked. if YES, all text cleared
 66 @property(nonatomic)        BOOL                    adjustsFontSizeToFitWidth; //字体自适应大小,设置为YES的时候文本会自动缩小以适应文本的大小,默认是保持原来的大小,而让长文本滚动 default is NO. if YES, text will shrink to minFontSize along baseline
 67 @property(nonatomic)        CGFloat                 minimumFontSize;      //自适应大小时,自动缩小显示的最小字体的大小 default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES
 68 @property(nullable, nonatomic,weak)   id<UITextFieldDelegate> delegate;             // default is nil. weak reference
 69 @property(nullable, nonatomic,strong) UIImage                *background;           // 背景 default is nil. draw in border rect. image should be stretchable
 70 @property(nullable, nonatomic,strong) UIImage                *disabledBackground;   // default is nil. ignored if background not set. image should be stretchable
 71 
 72 @property(nonatomic,readonly,getter=isEditing) BOOL editing;
 73 @property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // default is NO. allows editing text attributes with style operations and pasting rich text
 74 @property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes
 75 
 76 
 77 // You can supply custom views which are displayed at the left or right
 78 // sides of the text field. Uses for such views could be to show an icon or
 79 // a button to operate on the text in the field in an application-defined
 80 // manner.
 81 // 
 82 // A very common use is to display a clear button on the right side of the
 83 // text field, and a standard clear button is provided.
 84 
 85 @property(nonatomic)        UITextFieldViewMode  clearButtonMode; //文本后面的X号样式 sets when the clear button shows up. default is UITextFieldViewModeNever
 86 
 87 @property(nullable, nonatomic,strong) UIView              *leftView;        // e.g. magnifying glass
 88 @property(nonatomic)        UITextFieldViewMode  leftViewMode;    // sets when the left view shows up. default is UITextFieldViewModeNever
 89 
 90 @property(nullable, nonatomic,strong) UIView              *rightView;       // e.g. bookmarks button
 91 @property(nonatomic)        UITextFieldViewMode  rightViewMode;   // sets when the right view shows up. default is UITextFieldViewModeNever
 92 
 93 // drawing and positioning overrides
 94 
 95 - (CGRect)borderRectForBounds:(CGRect)bounds;
 96 - (CGRect)textRectForBounds:(CGRect)bounds;
 97 - (CGRect)placeholderRectForBounds:(CGRect)bounds;
 98 - (CGRect)editingRectForBounds:(CGRect)bounds;
 99 - (CGRect)clearButtonRectForBounds:(CGRect)bounds;
100 - (CGRect)leftViewRectForBounds:(CGRect)bounds;
101 - (CGRect)rightViewRectForBounds:(CGRect)bounds;
102 
103 - (void)drawTextInRect:(CGRect)rect;
104 - (void)drawPlaceholderInRect:(CGRect)rect;
105 
106 // Presented when object becomes first responder.  If set to nil, reverts to following responder chain.  If
107 // set while first responder, will not take effect until reloadInputViews is called.
108 @property (nullable, readwrite, strong) UIView *inputView;             
109 @property (nullable, readwrite, strong) UIView *inputAccessoryView;
110 
111 @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.
112 
113 @end
114 
115 #if TARGET_OS_IOS
116 
117 @interface UITextField () <UITextDraggable, UITextDroppable, UITextPasteConfigurationSupporting>
118 @end
119 
120 #endif
121 
122 @interface UIView (UITextField)
123 - (BOOL)endEditing:(BOOL)force;    // use to make the view or any subview that is the first responder resign (optionally force)
124 @end
125 
126 @protocol UITextFieldDelegate <NSObject>
127 
128 @optional
129 
130 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
131 - (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
132 - (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
133 - (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
134 - (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
135 
136 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
137 
138 - (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
139 - (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
140 
141 @end
142 
143 UIKIT_EXTERN NSNotificationName const UITextFieldTextDidBeginEditingNotification;
144 UIKIT_EXTERN NSNotificationName const UITextFieldTextDidEndEditingNotification;
145 UIKIT_EXTERN NSNotificationName const UITextFieldTextDidChangeNotification;
146 
147 UIKIT_EXTERN NSString *const UITextFieldDidEndEditingReasonKey NS_AVAILABLE_IOS(10_0);
148 
149 NS_ASSUME_NONNULL_END

//键盘样式

 text.keyboardType  = UIKeyboardTypeNumberPad;

typedef enum {

    UIKeyboardTypeDefault,       默认键盘,支持所有字符         

    UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

    UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

    UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

    UIKeyboardTypeNumberPad,              数字键盘

    UIKeyboardTypePhonePad,     电话键盘

    UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名

    UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘     

    UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点

    UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 

} UIKeyboardType;

//首字母是否大写

text.autocapitalizationType = UITextAutocapitalizationTypeNone;

typedef enum {

    UITextAutocapitalizationTypeNone, 不自动大写

    UITextAutocapitalizationTypeWords,  单词首字母大写

    UITextAutocapitalizationTypeSentences,  句子的首字母大写

    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写

} UITextAutocapitalizationType;

//return键变成什么键

  text.returnKeyType =UIReturnKeyDone;

 

typedef enum {

    UIReturnKeyDefault, 默认 灰色按钮,标有Return

    UIReturnKeyGo,      标有Go的蓝色按钮

    UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索

    UIReturnKeyJoin,标有Join的蓝色按钮

    UIReturnKeyNext,标有Next的蓝色按钮

    UIReturnKeyRoute,标有Route的蓝色按钮

    UIReturnKeySearch,标有Search的蓝色按钮

    UIReturnKeySend,标有Send的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyEmergencyCall, 紧急呼叫按钮

} UIReturnKeyType;

 

//键盘外观

textView.keyboardAppearance=UIKeyboardAppearanceDefault;

typedef enum {

UIKeyboardAppearanceDefault, 默认外观,浅灰色

UIKeyboardAppearanceAlert,     深灰 石墨色

 

} UIReturnKeyType;

posted @ 2018-10-25 18:17  CindyLi  阅读(261)  评论(0)    收藏  举报