请问如何在OC实现代码中,给UITableViewCell添加YYLabel,并在YYLabel中插入NSMutableAttributedString中的attachment,attachment中又嵌入UITextView,使得UITextView高度固定,内容可滚动,并且需要处理UITextView与TableView滚动冲突的问题。
时间: 2023-03-24 14:02:18 浏览: 154
您可以使用以下代码将YYLabel添加到UITableViewCell中,并在YYLabel中插入NSMutableAttributedString中的attachment,attachment中嵌入UITextView:
```
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(10, 10, cell.contentView.bounds.size.width - 20, )];
label.numberOfLines = ;
[cell.contentView addSubview:label];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"这是一段文字"];
// 创建attachment
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(, , cell.contentView.bounds.size.width - 20, 100)];
textView.text = @"这是一个UITextView";
textView.scrollEnabled = YES;
textView.backgroundColor = [UIColor clearColor];
textView.userInteractionEnabled = NO;
[textView sizeToFit];
NSAttributedString *attachment = [NSAttributedString attachmentStringWithContent:textView contentMode:UIViewContentModeScaleAspectFit attachmentSize:textView.bounds.size alignToFont:[UIFont systemFontOfSize:14] alignment:YYTextVerticalAlignmentCenter];
[attributedText appendAttributedString:attachment];
label.attributedText = attributedText;
[label sizeToFit];
return cell;
}
```
要处理UITextView与TableView滚动冲突的问题,您可以在UITextView上添加一个手势识别器,并将其禁用掉,这样就可以避免冲突了:
```
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
[textView addGestureRecognizer:tapGesture];
tapGesture.enabled = NO;
```
阅读全文