學習iOS控件之UITextField

學習iOS控件之UITextField

UITextField是一個文本輸入框,可以輸入字母,數字等。

UITextField的官網地址:https://developer.apple.com/reference/uikit/uitextfield 點擊打開鏈接

    //初始化
    UITextField *textField = [[UITextField alloc] init];
    
    //設置背景色
    textField.backgroundColor = [UIColor redColor];
    
    //設置frame
    textField.frame = CGRectMake(50, 50, 300, 50);
    
    //設置字體大小
    textField.font = [UIFont systemFontOfSize:15];
    
    //設置文本顏色
    textField.textColor = [UIColor blueColor];
    
    //在沒有輸入的情況,顯示的文字
    textField.placeholder = @"請輸入內容";
    
    /*
     右邊的小叉
     UITextFieldViewModeNever,          從不顯示
     UITextFieldViewModeWhileEditing,   編輯是現實(用的最多)
     UITextFieldViewModeUnlessEditing,  編輯時不現實
     UITextFieldViewModeAlways          總是現實
     */
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    /*
    NSTextAlignmentLeft      = 0,    // Visually left aligned   居左對齊

    NSTextAlignmentCenter    = 1,    // Visually centered       居中對齊
    NSTextAlignmentRight     = 2,    // Visually right aligned  居右對齊
    
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script  默認對齊,居左
    */
    //對齊方式
    textField.textAlignment = NSTextAlignmentJustified;
    
    //設置左邊視圖
    UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButton setImage:[UIImage imageNamed:@"bookmark"] forState:UIControlStateNormal];
    [overlayButton addTarget:self action:@selector(displayBookmarks:)
            forControlEvents:UIControlEventTouchUpInside];
    overlayButton.frame = CGRectMake(0, 0, 28, 28);
    textField.leftView = overlayButton;
    
    /*
     UIKeyboardTypeDefault,                // Default type for the current input method. 默認
     UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters ASCII碼
     UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation. 數組和符號
     UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
     UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). 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).
     UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.
     
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated 廢棄
     
     */
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    [self.view addSubview:textField];


代理的方法:

// return NO to disallow editing.
/**
 是否允許編輯

 @param textField textField description
 @return YES:允許編輯   NO:不想允許編輯
 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}


// became first responder
/**
 開始編輯

 @param textField textField description
 */
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}


// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
/**
 結束編輯

 @param textField textField description
 @return YES:允許結束編輯  NO:不允許借宿編輯
 */
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}


// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
/**
 結束編輯時調用

 @param textField textField description
 */
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}


// if implemented, called in place of textFieldDidEndEditing:
//textFieldDidEndEditing需要替換
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
    
}


// return NO to not change text
/**
 是否改變文本內容

 @param textField textField description
 @param range range description
 @param string string description
 @return YES
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}

// called when clear button pressed. return NO to ignore (no notifications)
/**
 按下clear時調用

 @param textField textField description
 @return YES:調用   NO:不調用
 */
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}

// called when 'return' key pressed. return NO to ignore.
/**
 按下return時調用

 @param textField textField description
 @return YES:調用  NO:不調用
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章