Swift : align UILabel text in the top rather in the middle

邮差的信 提交于 2019-12-18 11:33:30

问题


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

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()
}

回答1:


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




回答2:


Using Auto Layout in Storyboard will be very simple:

and

label.numberOfLines = 0;



回答3:


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

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

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




回答4:


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()
}


来源:https://stackoverflow.com/questions/31778316/swift-align-uilabel-text-in-the-top-rather-in-the-middle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!