前言:这个输入框基本可以解决textView使用过程中的大部分问题
1.调用,用起来也算方便
HYInputTextView *input = [[HYInputTextView alloc] initWithTitle:@"输入框测试" content:self.centerTextView.text andMaxCount:200 saveBlock:^(NSString * _Nonnull inputContent) {
self.centerTextView.text = inputContent;
}];
2.字数限制
- (void)setMax:(int)max {
_max = max;
_countLabel.text = [self countForContentString:self.content];
}
- (void)setContent:(NSString *)content {
_content = content;
self.inputView.text = content;
_countLabel.text = [self countForContentString:content];
[self updateSelfOfTextViewSize];
}
- (NSString *)countForContentString:(NSString *)content {
if (!content) content = @"";
return [NSString stringWithFormat:@"(%li/%i)",content.length,self.max];
}
- (void)textViewDidChange:(UITextView *)textView {
#pragma mark-----------注意次序
// 文字计算
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
//如果在变化中是高亮部分在变,就不要计算字符了
if (selectedRange && pos) {
return;
}
// 去掉英文没满一行就换行
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:INPUT_FONT]};
textView.attributedText = [[NSAttributedString alloc]initWithString:textView.text attributes:attributes];
if ((unsigned long)textView.text.length > self.max) {
// 对超出的部分进行剪切
textView.text = [textView.text substringToIndex:self.max];
} else {
//动态调整textView的高度
[self updateSelfOfTextViewSize];
}
self.countLabel.text = [self countForContentString:textView.text];
}
3.动态调整textView的高度
- (void)updateSelfOfTextViewSize {
//* TextView 大小*/
CGRect inputF = self.inputView.frame;
CGSize maxSize = CGSizeMake(inputF.size.width, CGFLOAT_MAX);
CGSize newSize = [self.inputView sizeThatFits:maxSize];
CGFloat maxHeight = MIN(INPUT_MAX_HEIGHT, newSize.height);
if (inputF.size.height ==maxHeight) return;
[self changeFrameForTextHeight:maxHeight];
//让text的内容平滑居中
[self.inputView scrollRangeToVisible:NSMakeRange(0, 0)];
}
- (void)changeFrameForTextHeight:(CGFloat)height {
CGRect inputF = self.inputView.frame;
inputF.size.height = height;
//改变自己的高度
CGRect rect = self.frame;
rect.size.height = self.oldHeight -TEXTVIEW_ACTION_HEIGHT + height;
rect.origin.y = rect.origin.y-(rect.size.height-self.frame.size.height);
// 改变backgrund高度
CGRect backF = self.centerBackgrundView.frame;
backF.size.height = rect.size.height -BACKGRUND_HEIGHT;
// 改变Button高度
CGRect buttonF = self.sureButton.frame;
buttonF.origin.y = CGRectGetMaxY(inputF) + INPUTVIEW_TOP;
[UIView animateWithDuration:.3 animations:^{
self.inputView.frame = inputF;
self.sureButton.frame = buttonF;
self.centerBackgrundView.frame = backF;
self.frame = rect;
}];
}
4.键盘弹出相关
#pragma mark - 键盘事件监听
- (void)initKeyboardObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification {
NSLog(@"键盘即将弹出");
NSValue* keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyboardBoundsValue CGRectValue];
NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
float animationDur = [keyboardAnimationDur floatValue];
_keyboardShowTime++;
// 第三方输入法有bug,第一次弹出没有keyboardRect
if (animationDur > 0.0f && keyboardRect.size.height == 0) {
_isThirdPartKeyboard = YES;
NSLog(@"第三方");
}
// 第三方输入法,有动画间隔时,没有高度
if (_isThirdPartKeyboard) {
// 第三次调用keyboardWillShow的时候 键盘完全展开
if (_keyboardShowTime == 3 && keyboardRect.size.height != 0 && keyboardRect.origin.y != 0) {
_keyboardFrame = keyboardRect;
//Animate change
[self showKeyBoardAnimation];
}
if (animationDur > 0.0) {
_keyboardAnimateDur = animationDur;
}
} else {
if (animationDur > 0.0) {
_keyboardFrame = keyboardRect;
_keyboardAnimateDur = animationDur;
[self showKeyBoardAnimation];
}
}
}
- (void)keyboardDidShow:(NSNotification*)notification {
_isKeyboardShow = YES;
if (_keyboardAnimateDur ==0) {
NSValue* keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
self.keyboardFrame = [keyboardBoundsValue CGRectValue];
_keyboardAnimateDur = 0.3f;
[self showKeyBoardAnimation];
}
}
- (void)keyboardWillHide:(NSNotification*)notification {
NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
float animationDur = [keyboardAnimationDur floatValue];
_isThirdPartKeyboard = NO;
_keyboardShowTime = 0;
if (animationDur > 0.0){
[_overView removeFromSuperview];
CGRect rect = self.frame;
rect.origin.y = MAINSCREEN_HEIGHT;
[UIView animateWithDuration:_keyboardAnimateDur animations:^{
self.frame = rect;
// [self layoutIfNeeded];
// [self layoutSubviews];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
}
- (void)keyboardDidHide:(NSNotification*)notification {
_isKeyboardShow = NO;
}
// 弹出视图
- (void)showKeyBoardAnimation {
CGRect rect = self.frame;
rect.origin.y = MAINSCREEN_HEIGHT- self.keyboardFrame.size.height-rect.size.height;
[UIView animateWithDuration:_keyboardAnimateDur animations:^{
self.frame= rect;
// [self layoutIfNeeded];
// [self layoutSubviews];
}];
}
网友评论