iOS—UITextField的使用方法


創建一個新窗體

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];

 

// UITextField的常用方法

 // 兩種創建方法

  UITextField *textField = [[UITextField alloc] init];

  UITextField *textField = [[UITextField alloc] initWithFrame:CGRextMake(50. 100, 200, 40)];

 

    // 最常用

  textField.frame = CGRectMake(3030260, 35);   // 設置位置 

  textField.backgroundColor = [UIColor grayColor];   // 設置背景顏色

  textField.alpha = 1.0;   // 設置透明度,范圍從0.0-1.0之間

  textField.textColor = [UIColor redColor];   // 設置文字的顏色

  textField.text = @"默認的文字";   // 設置默認的文字  

  textField.clearsOnBeginEditing = YES;       // 當第二次輸入的時候,清空上一次的內容

  textField.placeholder = @"這是提示文字";   // 顯示提示文件,當輸入文字時將自動消失  

 

  textField.font = [UIFont boldSystemFontOfSize:25.0f];   // 設置字體的大小

  textField.textAlignment = NSTextAlignmentCenter;   // 設置文字的對其方式

  // 對其的樣式如下

//    typedef NS_ENUM(NSInteger, NSTextAlignment) {

//        NSTextAlignmentLeft      = 0,    // Visually left aligned

//#if TARGET_OS_IPHONE

//        NSTextAlignmentCenter    = 1,    // Visually centered

//        NSTextAlignmentRight     = 2,    // Visually right aligned

//#else

//        NSTextAlignmentRight     = 1,    // Visually right aligned

//        NSTextAlignmentCenter    = 2,    // Visually centered

//#endif

//        NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.

//        NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script

//    } NS_ENUM_AVAILABLE_IOS(6_0);

    

 // 設置textField的樣式

  textField.borderStyle = UITextBorderStyleRoundedRect;  // 設置輸入框的樣式

  textField.minimumFontSize = 20.0f;   // 設置自動縮小顯示的最小字體大小    

  textField.tag = 1001;   // 設置textField的標簽,主要用在委托方法中

  textField.secureTextEntry = NO;   // 設置是否以密碼的圓點形式顯示

  textField.autocorrectionType = YES;   // 設置是否啟動自動提醒更新功能

  textField.returnKeyType = UIReturnKeyDefault;   // 設置彈出的鍵盤帶形式與帶的按鍵

  // 以下是彈出的鍵盤的帶按鈕的全部樣式

//    typedef NS_ENUM(NSInteger, UIReturnKeyType) {

//        UIReturnKeyDefault, // 默認的樣式

//        UIReturnKeyGo, // 帶一個go按鈕

//        UIReturnKeyGoogle, // 帶一個Search按鈕,使用google搜索

//        UIReturnKeyJoin, // 帶一個Join按鈕,登錄時使用

//        UIReturnKeyNext, // 帶一個Next按鈕,進入下一個輸入框

//        UIReturnKeyRoute, // 帶一個Route按鈕(路線)

//        UIReturnKeySearch, // 帶一個Search按鈕(搜索)

//        UIReturnKeySend, // 帶一個Send按鈕(發送)

//        UIReturnKeyYahoo, // 帶一個Search,使用yahoo搜索(搜索)

//        UIReturnKeyDone, // 帶一個Done按鈕(完成)

//        UIReturnKeyEmergencyCall, // 帶一個emergency call按鈕(緊急電話)

//    };

 

    textField.keyboardType UIKeyboardTypeNamePhonePad;   // 彈出的鍵盤的樣式

    // 以下是所有彈出的鍵盤的樣式

//    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 active

//        UIKeyboardTypeNumbersAndPunctuation,  // 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).

//#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED

//        UIKeyboardTypeDecimalPad,             // A number pad with a decimal point.

//#endif

//#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED

//        UIKeyboardTypeTwitter,                // A type optimized for twitter text entry (easy access to @ #)

//#endif

//        

//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

//        

//    };

    

  textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;   // 輸入的對其方法

  textField.autoresizingMask UIViewAutoresizingFlexibleHeight;   // 自動適應高度

  textField.clearButtonMode UITextFieldViewModeWhileEditing;   // 編輯的時候會顯示出刪除全部的小X

    // 出現刪除全部小X的時間

//    typedef enum {

//        UITextFieldViewModeNever // 重不出現

//        UITextFieldViewModeWhileEditing, // 編輯時出現

//        UITextFieldViewModeUnlessEditing, // 除了編輯外都出現

//        UITextFieldViewModeAlways  // 一直出現

//    } UITextFieldViewMode;

    

    //首字母是否大寫

  textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    // 以下是字母大寫的幾種情況

//    typedef enum {

//        UITextAutocapitalizationTypeNone, // 不自動大寫

//        UITextAutocapitalizationTypeWords, // 單詞首字母大寫

//        UITextAutocapitalizationTypeSentences, // 句子的首字母大寫

//        UITextAutocapitalizationTypeAllCharacters, // 所有字母都大寫

//    } UITextAutocapitalizationType;

    

    //鍵盤外觀

  textField.keyboardAppearance = UIKeyboardAppearanceAlert;

//    typedef enum {

//        UIKeyboardAppearanceDefault, // 默認外觀,淺灰色

//        UIKeyboardAppearanceAlert,   // 深灰 石墨色

//    } UIReturnKeyType;

        

    //最右側加圖片是以下代碼  左側類似

  UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_action_stat_share.png"]];

    // textField.rightView = image;

    // textField.rightViewMode = UITextFieldViewModeAlways;

  textField.leftView = image;

  textField.leftViewMode = UITextFieldViewModeAlways;

//    typedef enum {

//        UITextFieldViewModeNever,

//        UITextFieldViewModeWhileEditing,

//        UITextFieldViewModeUnlessEditing,

//        UITextFieldViewModeAlways

//    } UITextFieldViewMode;

    

    // 要想設置委托,就需要實現協議,需要在.h文件中添加協議

  textField.delegate = self;   // 設置委托

 

    // 添加到view上,並釋放內存

  [self.view addSubview:textField];

  [textField release], textField = nil;

 

 //當開始點擊textField會調用的方法

-(void)textFieldDidBeginEditing:(UITextField *)textField {

    NSLog(@"開始編輯");

}

//當textField編輯結束時調用的方法

-(void)textFieldDidEndEditing:(UITextField *)textField {

    NSLog(@"結束編輯");

}

//按下Done按鈕的調用方法,我們讓鍵盤消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

}

 

 

#pragma mark 應用程序即將暫停活躍狀態

- (void)applicationWillResignActive:(UIApplication *)application

{

    NSLog(@"程序即將暫停活躍狀態");

}

 

#pragma mark 應用程序已經進入后台

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    NSLog(@"應用程序已經進入后台");

}

 

#pragma mark 應用程序將要進入前台

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    NSLog(@"應用程序將要進入前台");

}

 

#pragma mark 應用程序已經變為活躍狀態

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    NSLog(@"應用程序已經變為活躍狀態");

}

 

#pragma mark 應用程序退出

- (void)applicationWillTerminate:(UIApplication *)application

{

    NSLog(@"應用程序退出");

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM