IOS基礎-UITextField

來源:互聯網
上載者:User

IOS基礎-UITextField

UITextField通常用於外部文字輸入,就是常見的輸入框.如下用代碼詳細說明UITextField的各種屬性和方法

 

- (void)viewDidLoad{    [super viewDidLoad];    //文本輸入框 UITextField    UITextField *textField = [[UITextField alloc] init];    //設定    textField.frame = CGRectMake(50, 50, 200, 200);        //設定輸入框中的文本 預設為空白    textField.text = nil;    //設定輸入框中的屬性字串,預設為空白 如果不理解屬性字串,可以跳過    textField.attributedText = nil;    //設定預設的屬性字串    textField.defaultTextAttributes = nil;    //設定預設顯示的屬性字串    textField.attributedPlaceholder = nil;    //設定輸入框的文本顏色,預設為空白,可不設定,因為系統設定為黑色    textField.textColor = [UIColor blackColor];    //設定背景顏色    textField.backgroundColor = [UIColor greenColor];    //設定輸入框的字型,預設為空白,可不設定,系統預設設定12pt    textField.font = [UIFont systemFontOfSize:18.0];    //設定背景圖片  只有當設定外框類型為UITextBorderStyleNone時才有效    //textField.background = [UIImage imageNamed:@"1.png"];    //textField.disabledBackground = [UIImage imageNamed:@"2.png"];        //設定輸入框變為密碼框  每輸入一個字元就變成點    textField.secureTextEntry = NO;    /*     typedef NS_ENUM(NSInteger, NSTextAlignment)     {     NSTextAlignmentLeft      = 0,      // 靠左對齊     #if   iphone     NSTextAlignmentCenter    = 1,    // 置中     NSTextAlignmentRight     = 2,    // 靠右對齊     #else  ipad     NSTextAlignmentRight     = 1,    // Visually right aligned     NSTextAlignmentCenter    = 2,    // Visually centered     #endif  其他     NSTextAlignmentJustified = 3,    // 和段落對齊     NSTextAlignmentNatural   = 4,    // 預設狀態  正常情況下     } NS_ENUM_AVAILABLE_IOS(6_0);     */    //設定輸入框的文字顯示模式    textField.textAlignment = NSTextAlignmentNatural;    //設定預設顯示的文字 70%透明度    textField.placeholder = @"請輸入文字";    /*     typedef NS_ENUM(NSInteger, UITextBorderStyle) {     UITextBorderStyleNone,       //無邊框     UITextBorderStyleLine,       //方角矩形 實線     UITextBorderStyleBezel,      //方角矩形 實線     UITextBorderStyleRoundedRect //圓角矩形 帶有透明度的線條     };*/    //設定外框類型    textField.borderStyle = UITextBorderStyleNone;        //設定輸入框字型最小值    textField.minimumFontSize = 18.0;        //設定為YES時文本會自動縮小以適應文字視窗大小,預設是保持原來大小,而讓長文本滾動    textField.adjustsFontSizeToFitWidth = NO;    /*     ypedef NS_ENUM(NSInteger, UIKeyboardType) {     UIKeyboardTypeDefault,                // 預設狀態,支援所有字元     UIKeyboardTypeASCIICapable,           // 可以輸入ASCII碼     UIKeyboardTypeNumbersAndPunctuation,  // 數字和標點符號     UIKeyboardTypeURL,                    // 字母和url(com)     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),    // 字母帶  前往 按鈕     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // 可以輸入ASCII碼 首字母大寫          };     */    //設定鍵盤的樣式    textField.keyboardType = UIKeyboardTypeURL;    //當再次編輯輸入框時 輸入框的內容就會被清空    textField.clearsOnBeginEditing = NO;        //首字母是否大寫    /*     typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {     UITextAutocapitalizationTypeNone,          //全部小寫     UITextAutocapitalizationTypeWords,         //每個單詞(中間有空格)的首字母大寫     UITextAutocapitalizationTypeSentences,     //第一個單詞的首字母大寫     UITextAutocapitalizationTypeAllCharacters, //全部大寫     };     */    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;        /*     輸入框中是否有個叉號,在什麼時候顯示,用於一次性刪除輸入框中的內          typedef NS_ENUM(NSInteger, UITextFieldViewMode) {     UITextFieldViewModeNever,         //預設顯示沒有     UITextFieldViewModeWhileEditing,  //輸入時顯示有叉號     UITextFieldViewModeUnlessEditing, //不輸入時顯示有叉號     UITextFieldViewModeAlways         //一直顯示有叉號,     };     */    textField.clearButtonMode = UITextFieldViewModeWhileEditing ;        /*     typedef enum {     UITextAutocorrectionTypeDefault, 預設     UITextAutocorrectionTypeNo,   不自動錯誤修正     UITextAutocorrectionTypeYes,  自動錯誤修正     } UITextAutocorrectionType;     */    //是否錯誤修正    textField.autocorrectionType = UITextAutocorrectionTypeNo;        /*     typedef NS_ENUM(NSInteger, UIReturnKeyType) {     UIReturnKeyDefault,       //return鍵顯示預設狀態 return     UIReturnKeyGo,            //return鍵顯示Go     UIReturnKeyGoogle,        //return鍵顯示Search     UIReturnKeyJoin,          //return鍵顯示Join 加入     UIReturnKeyNext,          //return鍵顯示next 下一個     UIReturnKeyRoute,         //return鍵顯示Route     UIReturnKeySearch,        //return鍵顯示Search 搜尋     UIReturnKeySend,          //return鍵顯示Send 發送     UIReturnKeyYahoo,         //return鍵顯示Search     UIReturnKeyDone,          //return建顯示Done     UIReturnKeyEmergencyCall,     };     */    //設定return鍵的類型    textField.returnKeyType = UIReturnKeyDefault;        /*     typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {     UIKeyboardAppearanceDefault,          //預設面板 淺灰色     UIKeyboardAppearanceDark              //深灰 石墨色     UIKeyboardAppearanceLight             //淺灰色     UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // 深灰 石墨色     };     */    //設定鍵盤的外觀    textField.keyboardAppearance = UIKeyboardAppearanceDefault;        //設定輸入文字之間位置的view    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];    image.bounds = CGRectMake(0, 0, 10, 10);    textField.leftView = image;    //顯示狀態和 叉號 顯示狀態類似    textField.leftViewMode = UITextFieldViewModeAlways;    //設定輸入框內部右側位置的view    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]];    image1.bounds = CGRectMake(0, 0, 10, 10);    textField.rightView = image1;    //顯示狀態和 叉號 顯示狀態類似    textField.rightViewMode = UITextFieldViewModeAlways;        textField.delegate = self;        [self.view addSubview:textField];}/*重寫繪製的各種方法  //重寫來重設邊緣地區 - (CGRect)borderRectForBounds:(CGRect)bounds;  //重寫來重設文字地區 - (CGRect)textRectForBounds:(CGRect)bounds;  //重寫來重設預留位置地區 也就是預設顯示的地區 - (CGRect)placeholderRectForBounds:(CGRect)bounds;  //重寫來重設編輯地區 - (CGRect)editingRectForBounds:(CGRect)bounds;  //重寫來重設clearButton位置,改變size可能導致button的圖片失真 - (CGRect)clearButtonRectForBounds:(CGRect)bounds;  //重寫來重設左邊view地區 - (CGRect)leftViewRectForBounds:(CGRect)bounds;  //重寫來重設右邊view地區 - (CGRect)rightViewRectForBounds:(CGRect)bounds;  //重寫繪製改變文字屬性,重寫時調用super可以按預設圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super - (void)drawTextInRect:(CGRect)rect;  //重寫繪製改變預留位置屬性,重寫時調用super可以按預設圖形屬性繪製,若自己完全重寫繪製函數,就不用調用super - (void)drawPlaceholderInRect:(CGRect)rect; */#pragma mark - UITextFieldDelegate//設定輸入框,是否可以被修改// NO將無法修改,不出現鍵盤// YES可以修改,預設值- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    return YES;}//開始編輯時獲得焦點時,執行該方法- (void)textFieldDidBeginEditing:(UITextField *)textField{}/* 返回BOOL值,指定是否允許文字欄位接觸,當編輯結束,文字欄位會讓出first responder 要想再使用者結束編輯時阻止文字欄位消失,可以返回NO 這對一些文字欄位必須始終保持活躍狀態的程式很有用,比如即使訊息 */- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    return NO;}//文字框結束編輯以後會調用- (void)textFieldDidEndEditing:(UITextField *)textField{}/* 當使用者使用自動校正功能,把輸入的文字修改為推薦的文字時,就會調用這個方法 這對於想要加入撤銷選項的應用程式特別有用 可以跟蹤欄位內所做的最後一次修改,也可以對所有編輯做日誌記錄,用做審計用途 要防止文字被改變可以返回NO 這個方法的參數中又一個NSRange對象,指明了被改變文字的位置,建議修改的文本也再其中 */- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    return YES;}//返回一個BOOL值指明是否允許根據使用者請求清除內容//可以設定在特定條件下才允許清除內容- (BOOL)textFieldShouldClear:(UITextField *)textField{    return YES;}//返回一個BOOL值,指明是否允許在按下斷行符號鍵時結束編輯//如果允許要調用resignFirstResponder方法,會導致結束編輯,而鍵盤會被手氣- (BOOL)textFieldShouldReturn:(UITextField *)textField{    return YES;}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.