AutoLayout row height miscalculating for NSAttributedString

后端 未结 8 1751
时光取名叫无心
时光取名叫无心 2021-01-31 11:57

My app pulls HTML from an API, converts it into a NSAttributedString (in order to allow for tappable links) and writes it to a row in an AutoLayout table. Trouble i

8条回答
  •  猫巷女王i
    2021-01-31 12:28

    You should be able to convert to an NSString to calculate the height like this.

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIFont * font = [UIFont systemFontOfSize:15.0f];
        NSString *text = [getYourAttributedTextArray objectAtIndex:indexPath.row] string];
        CGFloat height = [text boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width, maxHeight) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName: font} context:nil].size.height;
    
        return height + additionalHeightBuffer;
    }
    

提交回复
热议问题