9

I want the UILabel to start from the top even if the text is short it seems that NSTextAlignment doesn't work

enter image description here

cell.textContent.text = comments[indexPath.row]
cell.textContent.textAlignment = 




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //post's section == 0

    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
        cell.usernameLabel.text = "Steve Paul Jobs"
         cell.time.text = "9:42 PM"
         cell.commentsLabelCount.text = "12 Comments"
         cell.textContent.text = "Return the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectioReturn the number of rows in the sectio"

        cell.layoutSubviews()

    }

        let cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
        // Configure the cell...
      cell.layoutSubviews()

    cell.usernameLabel.text = "Steve Paul Jobs"
    cell.time.text = "9:42 PM"
    cell.textContent.text = comments[indexPath.row]
     cell.textContent.textAlignment = NSTextAlignment.Left


        return cell



}


import UIKit

class commentCell: UITableViewCell {
    @IBOutlet weak var textContent: UILabel!
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var usernameLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    userImage.layer.cornerRadius = 2
    userImage.clipsToBounds = true

}
override func layoutSubviews() {
    super.layoutSubviews()
    textContent.sizeToFit()
}
James Moriarty
  • 566
  • 2
  • 7
  • 27

4 Answers4

51

Using Auto Layout in Storyboard will be very simple:

enter image description here

and

label.numberOfLines = 0;
isaced
  • 1,735
  • 3
  • 16
  • 24
13

In your custom UITableViewCell class add this:

override func layoutSubviews() {
    super.layoutSubviews()
    textContent.sizeToFit()
}

Here's a link to a sample project just incase you want to reference how the cell and table is set up: https://mega.nz/#!ZoZCgTaA!7gvkRw4pwecMfDXrNW_7jR2dKe2UR9jPsq9tp_CRIcU

Caleb
  • 5,548
  • 3
  • 25
  • 32
  • Awesome but it changed only after I tap the each cell – James Moriarty Aug 03 '15 at 02:56
  • Hmmm. That's odd. It works for me without having to tap the cell. Would you mind posting the custom cell and the cellForRowAtIndexPath code? – Caleb Aug 03 '15 at 03:01
  • Try adding "return cell" inside the if statement of the cellForRowAtIndexPath function right after cell.layoutSubviews(). Nothing seems to be interfering with the label not updating properly. – Caleb Aug 03 '15 at 03:06
  • Sorry it is there but I forget to add the full code – James Moriarty Aug 03 '15 at 03:09
  • As it turns out, you don't even need "cell.layoutSubviews()." I am unable to determine why this still won't work though. Make sure you override the layoutSubviews function in both custom cells. – Caleb Aug 03 '15 at 03:14
  • I added a link to my sample project where I tested the code. I am afraid this is all the help I can provide without viewing the project. – Caleb Aug 03 '15 at 03:24
  • as you can see in the code above both are "override" – James Moriarty Aug 03 '15 at 03:24
7

This can easily be done with auto layout. Ensure your text label is in the correct view.

  1. Ensure your text label's number of lines is set to 0

number of lines is zero

  1. Select your text label and pin the top, left, and right constraints. Hit the 'Add 3 Constraints' button at the bottom.

enter image description here

  1. Update your frames (to see the updated alignment in storyboard).

enter image description here

David T
  • 2,724
  • 1
  • 17
  • 27
7

In your custom UITableViewCell class add this:

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentView.layoutIfNeeded() //This is the solution for :changed only after I tap the each cell
    textContent.sizeToFit()
}
Zeesha
  • 991
  • 9
  • 14