mark:
iOS:如何优雅的让UITextView根据输入文字实时改变高度
摘要分析:
1.这个是一边输入,一边改的
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGRect frame = textView.frame;
float height;
if ([text isEqual:@""]) {
if (![textView.text isEqualToString:@""]) {
// /*
// 按删除键的时候,text的文字为@"",导致删到换行时,并没有立马删,高度不同步,故强行删,但是容易越界,故要做判断
// */
height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
}else{
height = [ self heightForTextView:textView WithText:textView.text];
}
}else{
// /*
// textView.text和text,为了一输入就立马同步到textView里面,不等结束,从而解决输入和变高度不能立马同步
// */
height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
}
frame.size.height = height;
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
//这方法是要最后变一下父view的frame的
[weakSelf footerViewContent];
} completion:nil];
return YES;
}
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
CGRect size = [strText boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
context:nil];
float textHeight = size.size.height + 10.0;
return textHeight;
}
2.调用
调动这个方法是针对,一开始适配默认值尺寸的
- (void)updateAddressTextViewAndFooterViewHeight{
float height = [self heightForTextView:self.addressTextView WithText:self.addressTextView.text];
[self.addressTextView setYh_height:height];
[self footerViewContent];
}
3.无论哪种情况,最终都是要更新一下父view的frame的
封装版
h
#import <UIKit/UIKit.h>
@class XYCustomTextView;
@protocol XYCustomTextViewDelegate <NSObject>
- (void)customTextViewNewHeight:(CGFloat)height textView:(XYCustomTextView *)textView;
@end
@interface XYCustomTextView : UITextView
@property (assign, nonatomic) id<XYCustomTextViewDelegate> customTextViewDelegate;
//初始设置
- (instancetype)initWithFrame:(CGRect)frame
placeHolderText:(NSString *)placeHolderText
textAlignment:(NSTextAlignment)textAlignment
customTextViewDelegate:(id<XYCustomTextViewDelegate>)customTextViewDelegate;
//一开始要给这个值又要适配高度的情况
- (void)updateDescribeHeightWithDescribeForBegin:(NSString *)beginDescribe;
@end
m
#import "XYCustomTextView.h"
@interface XYCustomTextView()<UITextViewDelegate>
@property(strong, nonatomic) UILabel *textViewPlaceholderLabel;
@end
@implementation XYCustomTextView
//最小高度
#define minLineHeight 40
- (instancetype)initWithFrame:(CGRect)frame
placeHolderText:(NSString *)placeHolderText
textAlignment:(NSTextAlignment)textAlignment
customTextViewDelegate:(id<XYCustomTextViewDelegate>)customTextViewDelegate
{
if (self = [super initWithFrame:frame]) {
self.font = [UIFont systemFontOfSize:14];
self.textAlignment = textAlignment;
self.backgroundColor = [UIColor yellowColor];
//placeHolderLabel
_textViewPlaceholderLabel = [UILabel new];
_textViewPlaceholderLabel.font = [UIFont systemFontOfSize:14];
_textViewPlaceholderLabel.textAlignment = textAlignment;
_textViewPlaceholderLabel.textColor = [UIColor lightGrayColor];
_textViewPlaceholderLabel.text = placeHolderText;
self.delegate = self;
[self addSubview:_textViewPlaceholderLabel];
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
_textViewPlaceholderLabel.frame = CGRectMake(0, 0, self.frame.size.width - 5 , self.frame.size.height);
}
- (void)updateDescribeHeightWithDescribeForBegin:(NSString *)beginDescribe{
_textViewPlaceholderLabel.hidden = YES;
self.text = beginDescribe;
float height = [self heightForTextView:self WithText:beginDescribe];
//更新高度
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);
//更新外部的高度
[self updateOutsideHeight:height];
}
#pragma mark ------------------delegate------------
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"subView__text___%@",text);
CGRect frame = textView.frame;
_textViewPlaceholderLabel.hidden = YES;
if (range.length == 0 &&
range.location == 0 &&
[text isEqual:@""]){
_textViewPlaceholderLabel.hidden = NO;
}
float height;
if ([text isEqual:@""]) {
if (![textView.text isEqualToString:@""]) {
height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
}else{
height = [ self heightForTextView:textView WithText:textView.text];
}
}else{
height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
}
frame.size.height = height;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = frame;
} completion:nil];
//更新外部的高度
[self updateOutsideHeight:height];
return YES;
}
- (void)updateOutsideHeight:(CGFloat)height{
if ([_customTextViewDelegate respondsToSelector:@selector(customTextViewNewHeight:textView:)]){
[_customTextViewDelegate customTextViewNewHeight:height textView:self];
}
}
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
CGRect size = [strText boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
context:nil];
float textHeight = size.size.height + 10.0;
//最小值
if (textHeight < minLineHeight){
textHeight = minLineHeight;
}
return textHeight;
}
@end