第九篇 - UITextField
生活随笔
收集整理的這篇文章主要介紹了
第九篇 - UITextField
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
初始化
UITextField *tf = [[UITextField alloc] init];
?
typedef NS_ENUM(NSInteger, UITextBorderStyle) {//沒有任何邊框 UITextBorderStyleNone,//線性邊框 UITextBorderStyleLine,//陰影效果邊框 UITextBorderStyleBezel,//原型效果邊框 UITextBorderStyleRoundedRect };typedef NS_ENUM(NSInteger, UITextFieldViewMode) {//從不顯示 UITextFieldViewModeNever,//編輯的時候顯示 UITextFieldViewModeWhileEditing,//非編輯的時候顯示 UITextFieldViewModeUnlessEditing,//任何時候都顯示 UITextFieldViewModeAlways }; // //NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding> // //獲取和設置文字@property(nullable, nonatomic,copy) NSString *text; // default is nil //獲取和設置富文本@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil //文字的顏色@property(nullable, nonatomic,strong) UIColor *textColor; // default is nil. use opaque black // 文字的font@property(nullable, nonatomic,strong) UIFont *font; // default is nil. use system font 12 pt //對齊方式@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment //邊框樣式@property(nonatomic) UITextBorderStyle borderStyle; // default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored. //文字的大小,顏色等屬性統一設置,影響較廣泛,(全局)@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. // //占位文字@property(nullable, nonatomic,copy) NSString *placeholder; // default is nil. string is drawn 70% gray //富文本占位文字@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil // 編輯完一次后,再次回來編輯會清空上次編輯的所有內容(為yes時)@property(nonatomic) BOOL clearsOnBeginEditing; // default is NO which moves cursor to location clicked. if YES, all text cleared //自動調整字體大小@property(nonatomic) BOOL adjustsFontSizeToFitWidth; // default is NO. if YES, text will shrink to minFontSize along baseline //字體最小值(自動調整時)@property(nonatomic) CGFloat minimumFontSize; // default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES //代理@property(nullable, nonatomic,weak) id<UITextFieldDelegate> delegate; // default is nil. weak reference //背景圖片(會拉伸)@property(nullable, nonatomic,strong) UIImage *background; // default is nil. draw in border rect. image should be stretchable //不可用時背景圖片@property(nullable, nonatomic,strong) UIImage *disabledBackground; // default is nil. ignored if background not set. image should be stretchable // //是否在編輯狀態@property(nonatomic,readonly,getter=isEditing) BOOL editing; //是否允許更改字符屬性字典@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // default is NO. allows editing text attributes with style operations and pasting rich text //設置屬性字典@property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes // //// You can supply custom views which are displayed at the left or right //// sides of the text field. Uses for such views could be to show an icon or //// a button to operate on the text in the field in an application-defined //// manner. //// //// A very common use is to display a clear button on the right side of the //// text field, and a standard clear button is provided. // //設置清除按鈕的顯示模式@property(nonatomic) UITextFieldViewMode clearButtonMode; // sets when the clear button shows up. default is UITextFieldViewModeNever // //設置輸入框左邊的view@property(nullable, nonatomic,strong) UIView *leftView; // e.g. magnifying glass //設置輸入框左視圖的顯示模式@property(nonatomic) UITextFieldViewMode leftViewMode; // sets when the left view shows up. default is UITextFieldViewModeNever // @property(nullable, nonatomic,strong) UIView *rightView; // e.g. bookmarks button@property(nonatomic) UITextFieldViewMode rightViewMode; // sets when the right view shows up. default is UITextFieldViewModeNever // //// drawing and positioning overrides // - (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; // //// Presented when object becomes first responder. If set to nil, reverts to following responder chain. If //// set while first responder, will not take effect until reloadInputViews is called. //設置輸入框成為第一響應時彈出的視圖和輔助視圖(類似鍵盤)@property (nullable, readwrite, strong) UIView *inputView; //鍵盤上的bar(輔助工具條)@property (nullable, readwrite, strong) UIView *inputAccessoryView; // //這個屬性設置是否允許再次編輯時在內容中間插入內容@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. // @end // @interface UIView (UITextField)- (BOOL)endEditing:(BOOL)force; // use to make the view or any subview that is the first responder resign (optionally force)@end // @protocol UITextFieldDelegate <NSObject> // @optional // //點擊輸入框時觸發的方法,返回YES則可以進入編輯狀態,NO則不能。- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. //開始編輯時調用的方法- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder //將要結束編輯時調用的方法,返回YES則可以結束編輯狀態,NO則不能- (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 // //輸入字符時調用的方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text // //點擊清除按鈕時調用的函數,返回YES則可以清除,點擊NO則不能清除- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) //點擊return鍵觸發的函數- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore. // @end// //NS_ASSUME_NONNULL_END鍵盤代理
2016-03-18 07:54:42.866 UITextField[2479:169303] -[ViewController textFieldShouldBeginEditing:]
2016-03-18 07:54:42.880 UITextField[2479:169303] -[ViewController textFieldDidBeginEditing:]
2016-03-18 07:54:46.215 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:48.893 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:51.012 UITextField[2479:169303] -[ViewController textField:shouldChangeCharactersInRange:replacementString:]
2016-03-18 07:54:53.368 UITextField[2479:169303] -[ViewController textFieldShouldEndEditing:]
2016-03-18 07:54:53.375 UITextField[2479:169303] -[ViewController textFieldDidEndEditing:]
?
轉載于:https://www.cnblogs.com/kinghx/p/5290688.html
總結
以上是生活随笔為你收集整理的第九篇 - UITextField的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二叉树建立和遍历
- 下一篇: ecshop 快速添加会员