UITextField的基本使用

UITextField的基本使用

  • 有時候光標(biāo)距離textField的左邊太近 想調(diào)節(jié)一下距離:
    // 設(shè)置文本框左邊的內(nèi)容
    UIView *leftView = [[UIView alloc] init];
    leftView.frame = CGRectMake(0, 0, 10, 0);
    textField.leftView = leftView;
    //模式設(shè)置為一直顯示
    textField.leftViewMode = UITextFieldViewModeAlways;
  • 常見代理方法

     //是否允許開始編輯
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
    
     //是否允許結(jié)束編輯
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
    
     // 是否允許用戶輸入文字
    - (BOOL)textField:(UITextField *)textField 
         shouldChangeCharactersInRange:(NSRange)range 
                     replacementString:(NSString *)string;
    
    // 文本框開始編輯的時候調(diào)用
    - (void)textFieldDidBeginEditing:(UITextField *)textField;
    
  • 鍵盤彈出時的notification

  • 彈出的通知名稱

鍵盤狀態(tài)改變的時候,系統(tǒng)會發(fā)出一些特定的通知
UIKeyboardWillShowNotification // 鍵盤即將顯示
UIKeyboardDidShowNotification // 鍵盤顯示完畢
UIKeyboardWillHideNotification // 鍵盤即將隱藏
UIKeyboardDidHideNotification // 鍵盤隱藏完畢
UIKeyboardWillChangeFrameNotification//鍵盤的位置尺寸即將發(fā)生改變
UIKeyboardDidChangeFrameNotification // 鍵盤的位置尺寸改變完畢
  • 通知中包含的有用的信息
系統(tǒng)發(fā)出鍵盤通知時,會附帶一下跟鍵盤有關(guān)的額外信息(字典),字典常見的key如下:
UIKeyboardFrameBeginUserInfoKey // 鍵盤剛開始的frame
UIKeyboardFrameEndUserInfoKey // 鍵盤最終的frame(動畫執(zhí)行完畢后)
UIKeyboardAnimationDurationUserInfoKey // 鍵盤動畫的時間
UIKeyboardAnimationCurveUserInfoKey // 鍵盤動畫的執(zhí)行節(jié)奏(快慢)

鍵盤彈出和消失的時候屏幕的改變

  • 在鍵盤彈出和消失的時候一般控制器的view會作出相應(yīng)的改變,以便鍵盤不會遮擋住view
   - (void)viewDidLoad {
       [super viewDidLoad];
       // 監(jiān)聽鍵盤通知
       [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillChangeFrame:) 
                                                 name:UIKeyboardWillChangeFrameNotification 
                                               object:nil];
   }

   - (void)keyboardWillChangeFrame:(NSNotification *)notification {
       // 取出鍵盤最終的frame
       CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 取出鍵盤彈出需要花費的時間
    double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 修改transform
    [UIView animateWithDuration:duration animations:^{
        CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
        self.view.transform = CGAffineTransformMakeTranslation(0, - ty);
    }];
} 

UITextField的編輯事件的監(jiān)聽

  • 通過UIControl的addTarget方法
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
  • 通過代理
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
  • 通過通知
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(beginEditing) 
                                             name:UITextFieldTextDidBeginEditingNotification 
                                           object:textField];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(endEditing) 
                                             name:UITextFieldTextDidEndEditingNotification 
                                           object:textField];

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

  • 重寫UITextField的becomeFirstResponderresignFirstResponder方法
 //調(diào)用時刻 : 成為第一響應(yīng)者(開始編輯\彈出鍵盤\獲得焦點)
- (BOOL)becomeFirstResponder{
    return [super becomeFirstResponder];
}

//調(diào)用時刻 : 不做第一響應(yīng)者(結(jié)束編輯\退出鍵盤\失去焦點)
- (BOOL)resignFirstResponder{
 return [super resignFirstResponder];
}

UITextField的常見需求

  • 更改光標(biāo)的顏色
     textField.tintColor = [UIColor whiteColor];
    
  • 設(shè)置占位文字:設(shè)置placeholder或者attributedPlaceholder
  • 自定義占位文字顏色
  • 使用attributedPlaceholder進行設(shè)置
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder" attributes:attributes];
    
  • 重寫drawPlaceholderInRect方法
        //前提是先設(shè)置placeholder占位文字  否則這個方法不會走
      - (void)drawPlaceholderInRect:(CGRect)rect{
          // 文字屬性
          NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
          attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
          attrs[NSFontAttributeName] = self.font;
          CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);
          [self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs];
          // 畫出占位文字
          //    CGRect placeholderRect;
          //    placeholderRect.size.width = rect.size.width;
          //    placeholderRect.size.height = self.font.lineHeight;
          //    placeholderRect.origin.x = 0;
          //    placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5;
          //    [self.placeholder drawInRect:placeholderRect withAttributes:attrs];
      }
    
  • 修改內(nèi)部占位文字Label的文字顏色
[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
  • 在有多個textfield的時候在聚焦的時候占位文字顏色是一種顏色,非聚焦的時候是另一種顏色:實現(xiàn)方式是在成為第一響應(yīng)者的時候設(shè)置一次占位文字顏色,在市區(qū)第一響應(yīng)者的時候設(shè)置一次占位文字顏色
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容