-
设置内边距 kvc
效果图
image.png
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];
- 取消键盘响应:点击父类View 键盘下退(点击textView键盘不响应 所以textView也要添加手势)、点击父类View的黑色背景
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];
}
网友评论