直接上代码
HHArticleTextView *textView = [[HHArticleTextView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 34)];
self.textView = textView;
textView.textColor = [UIColor blackColor];
textView.font = [UIFont systemFontOfSize:TextFontSize];
textView.textViewPlacehold = @"呵呵";
textView.scrollEnabled = NO;
textView.showsVerticalScrollIndicator = NO;
textView.showsHorizontalScrollIndicator = NO;
textView.backgroundColor = [UIColor clearColor];
textView.delegate = self;
[self.contentView addSubview:textView];
if (@available(iOS 11.0, *)) {
textView.textDragInteraction.enabled = NO;
}
注:HHArticleTextView是我自定义的一个 textView 可以直接用系统的 UITextView
HHArticleEditingTextTableViewCell.h
@interface HHArticleEditingTextTableViewCell : UITableViewCell<UITextViewDelegate>
/**
*
*/
@property (nonatomic,strong) HHArticleTextView *textView;
/**
*
*/
@property (nonatomic,strong) HHArticleEditingModel *model;
/**
*
*/
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, readonly) CGFloat cellHeight;
@end
HHArticleEditingTextTableViewCell.m
#import "HHArticleEditingTextTableViewCell.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define kPadding 5
@implementation HHArticleEditingTextTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createUI];
}
return self;
}
- (void)createUI{
HHArticleTextView *textView = [[HHArticleTextView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 34)];
self.textView = textView;
textView.textColor = [UIColor blackColor];
textView.font = [UIFont systemFontOfSize:TextFontSize];
textView.textViewPlacehold = @"呵呵";
textView.scrollEnabled = NO;
textView.showsVerticalScrollIndicator = NO;
textView.showsHorizontalScrollIndicator = NO;
textView.backgroundColor = [UIColor clearColor];
textView.delegate = self;
[self.contentView addSubview:textView];
if (@available(iOS 11.0, *)) {
textView.textDragInteraction.enabled = NO;
}
}
- (CGFloat)cellHeight
{
return [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height + kPadding * 2;
}
- (void)setModel:(HHArticleEditingModel *)model{
_model = model;
self.textView.text = _model.text;
}
- (void)textViewDidChange:(UITextView *)TextView
{
// CGFloat newHeight = [self cellHeight];
self.textView.placeholdLabel.hidden = TextView.text.length>0;
_model.text = TextView.text;
CGRect frame = self.textView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [self.textView sizeThatFits:constraintSize];
self.textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
[_tableView beginUpdates];
[_tableView endUpdates];
}
@end
tableView创建的时候要加上下面三句不然 输入的时候 tableView 跳动的很厉害
//防止 tableView 输入时跳动
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
在这个方法里面要把 tableView 传到Cell 里面(cell.tableView = self.tableView;)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifierID = @"TextTableViewCell";
HHArticleEditingTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
if (!cell) {
cell = [[HHArticleEditingTextTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.tableView = self.tableView;
cell.model = model;
return cell;
}
在设置高的时候把获取到cell 的cellHeight属性直接赋值
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
HHArticleEditingTextTableViewCell * cell = (HHArticleEditingTextTableViewCell *)[tableView.dataSource tableView:_tableView cellForRowAtIndexPath:indexPath];
return cell.cellHeight<44 ? 44 : cell.cellHeight;
}
网友评论