How to consistently draw NSAttributedString in UITextView in UITableViewCell

后端 未结 1 2007
北海茫月
北海茫月 2021-01-13 01:57

I am having trouble getting consistent results from UITextViews in a UITableViewCell using NSAttributedStrings.

Inside - (UITableViewCell *)tableView:(UITabl

1条回答
  •  清酒与你
    2021-01-13 02:09

    When setting the attributed string of a UITextView that previously had a different attributed string, you must always set all of the UITextView's string-related properties to nil first, e.g.:

    self.tv.text = nil;
    self.tv.font = nil;
    self.tv.textColor = nil;
    self.tv.textAlignment = NSTextAlignmentLeft;
    self.tv.attributedText = s2;
    

    Otherwise, as you have discovered, old features of the previous attributed string still hang around and affect the new attributed string.

    In general, though, I have to say I don't see why you're using UITextView at all. If you don't need the user to be able to edit these attributed strings, use UILabel or else just draw the attributed string directly for the most accurate possible rendering. NSAttributedString gives you all the power you need to measure the size and draw within that size.

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