iOS开篇——UI之UITextField
生活随笔
收集整理的這篇文章主要介紹了
iOS开篇——UI之UITextField
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建文本輸入框
UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 250, 40)];設置邊框樣式
textField.borderStyle = UITextBorderStyleRoundedRect;/*typedef NS_ENUM(NSInteger, UITextBorderStyle) {UITextBorderStyleNone, 無效果UITextBorderStyleLine, 線性邊框 有陰影UITextBorderStyleBezel,UITextBorderStyleRoundedRect 圓角矩形};*/?
設置text相關
textField.text = @"2002年的第一場雪";//設置字體相關textField.font = [UIFont systemFontOfSize:20];textField.textColor = [UIColor greenColor];//設置自適應textField.adjustsFontSizeToFitWidth = YES;//設置居中 // textField.textAlignment = NSTextAlignmentCenter;?
設置提示語
//設置提示語textField.placeholder = @"請輸入";//設置是否清除 開始編輯時 // textField.clearsOnBeginEditing = YES;設置/取消第一響應者
//點擊輸入框 讓輸入框成為了第一響應者//所謂第一響應者 就是即將編輯的控件 要操作觸摸的控件 [textField becomeFirstResponder];//取消成為第一響應者[textField resignFirstResponder];交互是否開啟
//取消交互是否開啟textField.userInteractionEnabled = YES;設置清除按鈕
//設置清除按鈕textField.clearButtonMode = UITextFieldViewModeAlways;/*typedef NS_ENUM(NSInteger, UITextFieldViewMode) {UITextFieldViewModeNever, 從不UITextFieldViewModeWhileEditing, 編輯時UITextFieldViewModeUnlessEditing, 不編輯時顯示UITextFieldViewModeAlways 一直顯示};*/?
?
設置左右兩側view
//**********同一個view不能同時設置為左右圖片 // 左側viewUIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];view.backgroundColor = [UIColor blueColor];textField.leftView = view;textField.leftViewMode = UITextFieldViewModeAlways; // 右側viewUIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];textField.rightView = view1;view1.backgroundColor = [UIColor greenColor]; // textField.rightViewMode = UITextFieldViewModeAlways ;?
設置鍵盤樣式
//設置鍵盤樣式textField.keyboardType = UIKeyboardTypeDefault;/*typedef NS_ENUM(NSInteger, UIKeyboardType) {UIKeyboardTypeDefault, // Default type for the current input method.UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain activeUIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).UIKeyboardTypeNumberPad, // A number pad (0-9). 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).UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated};*/textField.returnKeyType =UIReturnKeyDone;/*typedef NS_ENUM(NSInteger, UIReturnKeyType) {UIReturnKeyDefault,UIReturnKeyGo,UIReturnKeyGoogle,UIReturnKeyJoin,UIReturnKeyNext,UIReturnKeyRoute,UIReturnKeySearch,UIReturnKeySend,UIReturnKeyYahoo,UIReturnKeyDone,UIReturnKeyEmergencyCall,UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),};*/?
觸摸屏幕 ?取消第一響應者 退出編輯
//觸摸屏幕 調用此方法 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{UITextField * textField = (id)[self.view viewWithTag:1];[textField resignFirstResponder]; // [self.view becomeFirstResponder]; }?
實現UITextFieldDelegate代理方法
#pragma mark - UITextFieldDelegate//是否可以開始編輯 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{return YES; }// return NO to disallow editing.//已經開始編輯 - (void)textFieldDidBeginEditing:(UITextField *)textField{NSLog(@"已經開始編輯"); }// became first responder//是否可以結束編輯 yes可以 no不可以 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{return YES; }// 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{//可在此進行保存草稿NSLog(@"已經結束編輯"); }// 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 YES; }// return NO to not change text//觸發此方法 返回可否清除 - (BOOL)textFieldShouldClear:(UITextField *)textField{return NO; }// called when clear button pressed. return NO to ignore (no notifications)- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{} //點擊鍵盤上的return 執行此方法 - (BOOL)textFieldShouldReturn:(UITextField *)textField{return YES; }// called when 'return' key pressed. return NO to ignore.?
轉載于:https://www.cnblogs.com/gwkiOS/p/4990259.html
總結
以上是生活随笔為你收集整理的iOS开篇——UI之UITextField的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: selenium随笔
- 下一篇: 根据 xsd 生成 jaxb java