1

Long time reader, first time poster. I have searched all over for a solution but I am yet to find an answer that works.

Ok the problem: I have a UILabel which gets text dynamically assigned to it. If I resize the label to cover the entire view controller the text is stuck in the middle. I found a bunch of solutions which say to add the following code:

detailsLabel.numberOfLines = 0;
detailsLabel.sizeToFit()

However this doesn't do anything.

Can someone have a look at my code and provide suggestions?

import UIKit

class DetailsViewController: UIViewController {

@IBOutlet weak var detailsLabel: UILabel!
@IBOutlet weak var lbldetails: UILabel!


var selectedBeach : Beach?


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    detailsLabel.text = selectedBeach?.greaterDetails
    lbldetails.text = (selectedBeach?.beachName ?? "") + " Details"

    detailsLabel.numberOfLines = 0;
    [detailsLabel .sizeToFit()]

}

@IBAction func dismiss(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
Flexo
  • 95
  • 2
  • 5

3 Answers3

2

ok. do the following:

  1. drag LEFT from the label to the view, release and choose Leading Space to Container Margin
  2. drag RIGHT from the label to the view, release and choose Trailing Space to Container Margin
  3. drag UPWARDS from the label to the view, release and choose Top Space to Top Layout Guide

after that go to the label's size inspector and make sure that the constraints all have a constant of Standard.

last but not least go to the label's attributes inspector and make sure that the Lines property is set to 0.

feel free to ask if you have any questions :)

André Slotta
  • 13,774
  • 2
  • 22
  • 34
0

The correct code in Swift is

detailsLabel.sizeToFit()

Without the [ ]

And for the Objective-C it's

[detailsLabel sizeToFit];
Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30
  • Hi Stefan Salatic, Thanks for the quick response, I have updated the code with your suggestion however I get the exact same result. – Flexo May 05 '15 at 10:51
  • Hi, if you need more information you can check out this complete answer http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel?rq=1 – Stefan Salatic May 05 '15 at 10:52
0

You can do the following things to enable UILabel that support multiline.

 lbldetails.numberOfLines = 0
 lbldetails.preferredMaxLayoutWidth = lbldetails.frame.size.width

Here i am consider that your UILable support dynamic height as per its text just issue regarding not displaying multiline.

Kindly give following constraints to UILabel. enter image description here

Hope this help you.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43