0

I'm using XIB to populate tableview of a chat app (messages). It contains a view and a label on it. The label's text continuously changes. How can I find the width of label and then set width of parent view equal to that?

I'm a newbie to swift. So, in this way, is it possible to set height also?

I have set the number of lines to '0' of the uilabel enter image description here

Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122

1 Answers1

2

Assuming that the "Message Text" label is the only subview within the parent view, you could set the parent view's height and width like so:

if let subview = view.subviews.first {
    // where `view` is your parent view and `subview` is your text label
    view.frame.size.width = subview.frame.width
    view.frame.size.height = subview.frame.height  
}
Aaron
  • 6,466
  • 7
  • 37
  • 75