美文网首页
UIKit-UITextField

UIKit-UITextField

作者: MrDemon_ | 来源:发表于2020-03-24 22:32 被阅读0次

    UIKit系列常见处理办法集合

    点击任何地方收缩键盘

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self setUpForDismissKeyboard];
    }
    
    - (void)setUpForDismissKeyboard {
      NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
      UITapGestureRecognizer *singleTapGR =
      [[UITapGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(tapAnywhereToDismissKeyboard:)];
      NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
      [nc addObserverForName:UIKeyboardWillShowNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                  }];
    
      [nc addObserverForName:UIKeyboardWillHideNotification
                      object:nil
                       queue:mainQuene
                  usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                  }];
    }
    
    - (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
      [self.view endEditing:YES];
    }
    

    return 回收键盘

    //实现代理方法
    -(BOOL) textFieldShouldReturn:(UITextField *)textField{
        if (textField == self.addTextField) {    
            [textField resignFirstResponder];
        }
        return YES;
    }
    

    修改textField的placeholder的字体颜色、大小

     [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
    
     [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];  
    

    水印颜色

    //上面这句是设置placeHolder的颜色,placeHolderColor是自己需要的颜色
    userNameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号码"
                                        attributes:@{ NSForegroundColorAttributeName:placeHolderColor}];
    

    UITextField实现左侧空出一定的边距

    -(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth{
        CGRect frame = textField.frame;
        frame.size.width = leftWidth;
        UIView *leftview = [[UIView alloc] initWithFrame:frame];
        textField.leftViewMode = UITextFieldViewModeAlways;
        textField.leftView = leftview;
    }
    

    右边距

    //写一个类继承UITextField,重写这个方法:
    - (CGRect) rightViewRectForBounds:(CGRect)bounds {
       CGRect textRect = [super rightViewRectForBounds:bounds];
       textRect.origin.x -= 10;
       return textRect;
    }
    //然后需要的地方,用你自己写的这个类替换UITextField。
    

    视图随键盘提升

    -(void)keyboardWillShow:(NSNotification *)sender{
        CGRect frame = _loadButton.frame;
        int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216);//键盘高度216
        if (offset >= 150) {
            offset = 100;
        }
        NSTimeInterval animationDuration = 0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        offset = offset + 70;
        //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
    
    -(void)keyboardWillHidden:(NSNotification *)sender{
        if (self.isKeyBoard == NO) {
            if (IOS7) {
                self.view.frame =CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
            }else {
                self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            }
        }
    }
    

    文字置顶

    self.automaticallyAdjustsScrollViewInsets = NO;
    

    相关文章

      网友评论

          本文标题:UIKit-UITextField

          本文链接:https://www.haomeiwen.com/subject/kyfzyhtx.html