YYText设置内边距+键盘处理

2019-05-21  本文已影响0人  守护地中海的花
       YYTextView *view = [[YYTextView alloc]init];
        [self addSubview:view];
        view.backgroundColor = kColor180;
        view.placeholderFont = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightRegular];
        view.placeholderText = @"删除话题原因~";
        view.placeholderTextColor = kColor160;
        view.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightRegular];
        view.textColor = [UIColor blackColor];
        view.frame = CGRectMake(13*ADAPTER_WIDTH, 98*ADAPTER_WIDTH, self.width - 26*ADAPTER_WIDTH, 206*ADAPTER_WIDTH);
        view.layer.cornerRadius = 5*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        /*
         @property (nonatomic) IBInspectable CGFloat insetTop_;
         @property (nonatomic) IBInspectable CGFloat insetBottom_;
         @property (nonatomic) IBInspectable CGFloat insetLeft_;
         @property (nonatomic) IBInspectable CGFloat insetRight_;
         */
        [view setValue:@(20*ADAPTER_WIDTH) forKey:@"insetTop_"];
        [view setValue:@(20*ADAPTER_WIDTH) forKey:@"insetBottom_"];
        [view setValue:@(8*ADAPTER_WIDTH) forKey:@"insetLeft_"];
        [view setValue:@(8*ADAPTER_WIDTH) forKey:@"insetLeft_"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTextView:)];
        view.userInteractionEnabled = YES;
        [view addGestureRecognizer:tap];
@property(nonatomic,assign)BOOL keyboardJump;//YES 键盘弹起 NO 键盘没有弹起

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

#pragma mark - 处理键盘
- (void)keyboardWillChangeFrame:(NSNotification *)noti
{
    CGRect keyBoardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    //0.250000
    CGFloat moreMargin = 103*ADAPTER_WIDTH;
    CGFloat top = keyBoardFrame.origin.y == HEIGHT ? (HEIGHT - kSelfHeight) : (HEIGHT - kSelfHeight - keyBoardFrame.size.height + moreMargin);
    self.keyboardJump = keyBoardFrame.origin.y == HEIGHT ? NO : YES ;
    [UIView animateWithDuration:duration animations:^{
        self.top = top;
    }];
}

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSelf:)];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:tap];


- (void)clickSelf:sender
{
    [self.textView resignFirstResponder];
}
- (void)clickTextView:sender
{
    [self.textView becomeFirstResponder];
}
上一篇 下一篇

猜你喜欢

热点阅读