AutoLayout row height miscalculating for NSAttributedString

后端 未结 8 1615
时光取名叫无心
时光取名叫无心 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条回答
  • 2021-01-31 12:38

    You need to update intrinsic content size.

    I assume that you set attributed text to label in this code [self configureContentCellForIndexPath:cell atIndexPath:indexPath];

    So, it should look like this

    cell.youLabel.attributedText = NSAttributedString(...) 
    cell.youLabel.invalidateIntrinsicContentSize()
    cell.youLabel.layoutIfNeeded()
    

    You height calculation code (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andFont:(UIFont *)font andWidth:(CGFloat)width should be replaced with cell height calculation using prototyping cell.

    0 讨论(0)
  • 2021-01-31 12:42

    I ran into a very similar issue on another project where fields using NSAttributedString weren't rendering with the correct height. Unfortunately, there are two bugs with it that made us completely drop using it in our project.

    The first is a bug that you've noticed here, where some HTML will cause an incorrect size calculation. This is usually from the space between the p tags. Injecting CSS sort of solved the issue, but we had no control over the incoming format. This behaves differently between iOS7 and iOS8 where it's wrong on one and right on the other.

    The second (and more serious) bug is that NSAttributedString is absurdly slow in iOS 8. I outlined it here: NSAttributedString performance is worse under iOS 8

    Rather than making a bunch of hacks to have everything perform as we wanted, the suggestion of using https://github.com/Cocoanetics/DTCoreText worked out really well for the project.

    0 讨论(0)
提交回复
热议问题