0

I'm creating a chat based app and on the conversation thread, I am using a UIView with a layout constraint of trailing to superview of 46px to ensure the chat bubble will be at least 46px from the right of the table view cell. Within the UIView I have a child UITextView which is constrained to the parent view on the top, bottom, and left. Since the text view's width can vary, I did not put a constraint on the right.

How can I get the UITextView to wrap it's text within the bounds of the UIView? If I don't have the clip subviews enabled on the UIView, the longer messages overflow the view and go past the end of the table view cell. If I have the clip subviews enabled, the longer messages are just clipped and don't get wrapped.

If I use a constraint on the trailing end of the UITextView, the width of the text view doesn't change. This makes the chat bubble much longer than the message.

Could anyone help point me in the right direction?

Mike Walker
  • 2,944
  • 8
  • 30
  • 62
  • To clarify, you're putting chat bubbles _inside_ of a UITableViewCell? – Aaron Apr 21 '15 at 17:27
  • Yes that's correct. I have a UITableView cell with one of the views inside is the UIView with the UITextView as it's child. – Mike Walker Apr 21 '15 at 17:30
  • For the `UITextView` in the attributes inspector, do you have scrolling enabled? – Aaron Apr 21 '15 at 17:43
  • No the scrolling enabled is not checked – Mike Walker Apr 21 '15 at 17:55
  • Enable scrolling and see what happens. – Aaron Apr 21 '15 at 18:26
  • When I enable scrolling, the table view cell height is too small to see what is inside the `UITextView`. – Mike Walker Apr 21 '15 at 19:25
  • You are already heavily constrained if you're putting chat bubbles in table view cells. The only way to maximize a textview without scrolling is to make the text as small of a font as possible and/or maximize the textview inside of the constraints. If you don't want scrolling or editing functionality, I would suggest using a UILabel instead of a UITextView or UITextField if you're looking only to display un-editable text (which in a table view cell should be the case) and then set the UILabel's lines attribute to the number of lines you'd like displayed. – Aaron Apr 21 '15 at 21:00

1 Answers1

0

Thank for you the assistance. I ended up using UILabel. I found out I shouldn't be using sizeToFit(), but instead set preferredMaxLayoutWidth for the UILabel. This post really helped me UILabel sizeToFit and constraints.

Community
  • 1
  • 1
Mike Walker
  • 2,944
  • 8
  • 30
  • 62