我的TableViewCell.xib中有3个UIlabel。其中一个需要使用NSMutableAttributedString设置attributedText,代码如下:
NSString * htmlString = [NSString stringWithFormat:@"将订单状态从 %@ 改变为 %@ ,原因:%@",lb.order_status,lb.changed_status,lb.remark];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.lineHeightMultiple = 0;
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, attrStr.length)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Palatino-Roman" size:13.0] range:NSMakeRange(0, attrStr.length)];
lbRemark.attributedText=attrStr;
问题1: lbRemark不能用多行显示,但如果我不使用NSAttributedString来设置lbRemark,那么它可以显示多行。是不是NSAttributedString有问题?
问题2:我需要我的手机用lbRemark'来计算它的身高。高度。
答案 0 :(得分:0)
1:将此设置为多行:
lbRemark.numberOfLines = 0;
2:这是根据文字计算标签高度的方法:
NSString * htmlString = [NSString stringWithFormat:@"将订单状态从 %@ 改变为 %@ ,原因:%@",@"test",@"test1",@"test2"];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.lineHeightMultiple = 0;
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, attrStr.length)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Palatino-Roman" size:13.0] range:NSMakeRange(0, attrStr.length)];
lbRemark.numberOfLines = 0;
CGRect paragraphRect = [attrStr boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil]; //here 625 is width of label, use whatever your width
NSLog(@"height = %f", paragraphRect.size.height); //it should be height for label
lbRemark.attributedText=attrStr;
答案 1 :(得分:0)
从iOS8开始,您不需要计算标签的高度。自动布局将为您做到这一点。
在UILabel和单元格之间附加顶部和底部约束 - 不要为UILabel设置高度约束。
在UIViewController的viewDidLoad:方法中,只需执行以下操作(将50.0f替换为行的默认高度):
self.tableView.estimatedRowHeight = 50.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
答案 2 :(得分:0)
static UITableViewCell *sizingLogCell = nil;
static dispatch_once_t onceToken;
if ([indentifier isEqualToString:@"logCell"]) {
dispatch_once(&onceToken, ^{
sizingLogCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
});
sizingLogCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(tableView.bounds));
[sizingLogCell setNeedsLayout];
[sizingLogCell layoutSubviews];
[self configureLogCell:sizingLogCell forIndexPath:indexPath];
CGSize size = [sizingLogCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
该功能的名称是: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath indentifierForCell:(NSString *)indentifier
最重要的是' size.height + 1 '如果没有' + 1'标签仍然无法用多行显示。