请问如何在OC实现代码中,给UITableViewCell添加YYLabel,并在YYLabel中插入NSMutableAttributedString中的attachment,attachment中又嵌入UITextView,使得UITextView高度固定,内容可滚动,并且需要处理UITextView与TableView滚动冲突的问题。
时间: 2023-03-23 15:04:58 浏览: 58
您好,可以使用以下代码实现:
1. 在UITableViewCell的初始化方法中,创建YYLabel并添加到cell.contentView中:
```
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:label];
self.yyLabel = label;
}
return self;
}
```
2. 在设置UITableViewCell的数据源方法中,设置YYLabel的frame和内容:
```
- (void)setModel:(Model *)model {
_model = model;
self.yyLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width - 20, model.textLayout.textBoundingSize.height);
self.yyLabel.attributedText = model.textLayout.text;
}
```
3. 在NSMutableAttributedString中插入attachment时,可以使用YYTextAttachment对象,并将UITextView添加到attachment中:
```
YYTextAttachment *attachment = [YYTextAttachment new];
attachment.content = textView;
attachment.contentMode = UIViewContentModeScaleAspectFit;
attachment.contentInsets = UIEdgeInsetsMake(, , , );
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attachmentString atIndex:insertIndex];
```
4. 处理UITextView与TableView滚动冲突的问题,可以在UITextView的父视图中重写以下方法:
```
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([self pointInside:point withEvent:event]) {
self.superview.userInteractionEnabled = NO;
} else {
self.superview.userInteractionEnabled = YES;
}
return [super hitTest:point withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.superview.userInteractionEnabled = YES;
[super touchesEnded:touches withEvent:event];
}
```
希望以上代码对您有所帮助。