2

I have a non-editable UITextView object inside a UITableViewCell with appropriate constraints. It is loaded as follows:

.h:

@property (weak, nonatomic) IBOutlet UITextView *textContentText;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textContentTextHeight;

.m:

-(void)configureForTextBlock:(LessonBlock *)block {

    self.textContentText.text = block.sectionContent;
    self.textContentText.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

    CGSize sizeThatFitsTextView = [self.textContentText sizeThatFits:self.textContentText.frame.size];
    self.textContentTextHeight.constant = sizeThatFitsTextView.height;

    [self.textContentText layoutIfNeeded];
    [self.textContentText updateConstraints];
}

The cell and the UITextView resize fine (height only), but there are more lines of (blank) text dynamically added to the UITextView than needed at the bottom.

Can anyone help please?

Pang
  • 9,564
  • 146
  • 81
  • 122
NickT
  • 31
  • 1
  • I would use a multiline UILabel if I was you because you don't have to worry about having a constraint controlling its height. – ABakerSmith Apr 01 '15 at 08:54
  • I'd ideally like the text to be selectable, and I don't think you can do this with a UILabel (correct me if I'm wrong)? – NickT Apr 01 '15 at 12:08
  • You are correct, you can't have a selectable UILabel... – ABakerSmith Apr 01 '15 at 12:12
  • I tested your code and the problem seemed to be conflicting constraints. Do you have messages in the console too? – ABakerSmith Apr 01 '15 at 12:13
  • My UITextView is in a UITableViewCell (in a separate .nib). Constraints on the UITextView are as follows: Height: 100 (priority 800) Four constraints - leading, trailing, top, and bottom (all priority 1000) to content view of the uitableviewcell... No errors in the console. If I remove any of these constraints, then I get a blank cell. It seems like the problem is to do with sizeThatFits making the UITextView too big for the content! – NickT Apr 01 '15 at 12:40
  • When are you calling `configureForTextBlock`? The problem may be that the constraints haven't been evaluated yet and so using `self.textContentText.frame.size` is incorrect. Try setting the `textContentTextHeight.constant` in `updateConstraints` after calling `super.updateConstraints`. – ABakerSmith Apr 01 '15 at 13:16
  • @ABakerSmith Hi, I tried doing this, and it still appears exactly the same... See: http://tinypic.com/r/16ib1g7/8 – NickT Apr 02 '15 at 02:34
  • @NickT I have the same problem, Did you fix it? – Gabriel Goncalves Apr 15 '16 at 16:42
  • **HERE** ... http://stackoverflow.com/a/19704178/294884 – Fattie Feb 24 '17 at 04:07

0 Answers0