// // TestTableViewCell.m // TestUITextView // // Created by xxx on 2022/6/8. // #import "TestTableViewCell.h" @interface TestTableViewCell () @property (nonatomic , strong) UITextView *textView; @property (nonatomic , strong) NSLayoutConstraint *cstrWidth; @property (nonatomic , strong) NSLayoutConstraint *cstrHeight; @end @implementation TestTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self setupTextView]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)setupTextView { self.textView = [[UITextView alloc] init]; self.textView.editable = NO; self.textView.scrollEnabled = NO; self.textView.backgroundColor = [UIColor lightGrayColor]; self.textView.textColor = [UIColor darkTextColor]; self.textView.contentInset = UIEdgeInsetsZero; self.textView.textContainerInset = UIEdgeInsetsZero; self.textView.textContainer.lineFragmentPadding = 0; self.textView.showsVerticalScrollIndicator = NO; // by accessing layoutManager,it is working by 'fall back' to TextKit 1 // self.textView.layoutManager; [self.contentView addSubview:self.textView]; // use NSLayoutConstraint,it is working // self.textView.translatesAutoresizingMaskIntoConstraints = NO; // [NSLayoutConstraint activateConstraints:@[ // [self.textView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:20], //// [self.textView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-20], // [self.textView.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:10], // ]]; // // self.cstrHeight = [self.textView.heightAnchor constraintEqualToConstant:10]; // self.cstrHeight.active = YES; // self.cstrWidth = [self.textView.widthAnchor constraintEqualToConstant:10]; // self.cstrWidth.active = YES; //use frame,not working self.textView.frame = CGRectMake(10, 20, self.contentView.bounds.size.width - 20, self.contentView.bounds.size.height-40); } - (void)bindModel:(TestModel *)model { // use NSLayoutConstraint,it is working // self.cstrWidth.constant = model.size.width; // self.cstrHeight.constant = model.size.height - 40; CGRect frame = self.textView.frame; frame.size.width = model.size.width; frame.size.height = model.size.height-40; self.textView.frame = frame; // NSLog(@"test cell:%p,textView:%p,%p",self,self.textView.attributedText,model.attrbutedText); // when use frame ,after clearing by setting nil,it will be working // self.textView.attributedText = nil; self.textView.attributedText = model.attrbutedText; } @end