0

This seems like it would be intuitive, but I've been trying to get text to stop going off screen for 45 minutes. Here is my viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    orgTitle.numberOfLines = 0
    orgTitle.lineBreakMode = .byWordWrapping
    orgTitle.frame.size.width = self.view.frame.size.width - 20
    orgTitle.sizeToFit()

    orgDesc.numberOfLines = 0
    orgDesc.lineBreakMode = .byWordWrapping
    orgDesc.frame.size.width = self.view.frame.size.width - 20
    orgDesc.sizeToFit()

    self.orgTitle.text = self.oTitle
    self.orgDesc.text = self.desc
    self.orgImage.image = self.image
}

I attached a screenshot of the issue as well as the settings for my label. Label Settings Screenshot of issue

Gabe Spound
  • 146
  • 2
  • 14
  • 2
    Use your debugger. Look at the size of the frame in `viewDidLoad`. It's too soon to rely on the frame. – rmaddy Feb 01 '17 at 02:30
  • I don't think that's it. That was my most recent addition and it didn't change anything. I have since removed it and nothing changed. – Gabe Spound Feb 01 '17 at 02:35

1 Answers1

2

I find it a lot easier to use constraints to set layout rather than manually adjusting the frame...

But if, for whatever reason, you want to manually set the frame of the label I'd suggest two things:

  1. Like like @rmaddy mentions, viewDidLoad is too early in the view controller lifecycle for the frame to be correct. Try overriding layoutSubviews and moving the frame adjustments into that method.

  2. For a UILabel with numberOfLines set to 0, you'll also want to set the labels preferredmaxlayoutwidth property to help it figure out how many lines it needs to be (not needed if using constraints).

Also, if you're willing to target iOS9 and above, UIStackView is really nice addition to UIKit to help with this sort of layout where subviews get pushed down from potential increasing height of multi-line labels.

Here's some screen grabs of adding a stack view to hold 2 UILabels, and then constraints that pin the stackview's left, top, and right edges to the superview:

enter image description here

With the layout defined in your storyboard, your viewDidLoad becomes a lot simpler with just setting the actual content for the labels:

override func viewDidLoad() {
    super.viewDidLoad()

    self.orgTitle.text = self.oTitle
    self.orgDesc.text = self.desc
    self.orgImage.image = self.image
}
MathewS
  • 2,267
  • 2
  • 20
  • 31
  • I would much rather not set the frame, that's just what I tried most recently. How do I do it otherwise? – Gabe Spound Feb 01 '17 at 03:58
  • @EthanKlein updated answer with some screen-grabs of adding stack view and constraints in your storyboard. – MathewS Feb 01 '17 at 04:12
  • I am done setting up my stack view, Thank you for the incredibly in depth guide by the way, and I'm just wondering if now I have to go back and change the line number / line Break settings of my labels? – Gabe Spound Feb 01 '17 at 04:36
  • 1
    Nevermind, I got it. I just left my line settings. Thank you so much. – Gabe Spound Feb 01 '17 at 04:38