0

I have a customView contain some subViews like this:

enter image description here

I have set messageLabel trailing constraint to it's superView = 4

I want my customView width constraint will be set automatically base on messageLabel. If I create customView like this:

FlyingMessageDisplayView(frame: CGRect(x: UIScreen.main.bounds.width, y: 0, width: 100, height: 40))

how to set it's width size because messageLabel can be long or sort.

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
  • you need to it manually, first find width of label with it text and set view size accordingly see this how to find width http://stackoverflow.com/questions/1340667/pixel-width-of-the-text-in-a-uilabel – Prashant Tukadiya Jan 17 '17 at 04:58
  • Why would you want to pass the frame if you want it to automatically resize? – Rikh Jan 17 '17 at 05:01

1 Answers1

0

I set my customView width size base on it's sub label like this

customView.frame.size.width = customView.messageLabelWidth + 50 // 50 here is for imageView width and some spacing.

where messageLabelWidth in FlyingMessageDisplayView

var messageLabelWidth: CGFloat {
    get {
        return messageLabel.intrinsicContentSize.width
    }
}

It will calculate the width of label first base on text length, then update width of customView

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116