Swift:在顶部而不是中间对齐UILabel文本

我想UILabel从顶部开始,即使文本很短,似乎NSTextAlignment不起作用

在这里输入图像描述

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

在你的自定义UITableViewCell类中添加这个:

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

这里是一个示例项目的链接,只是想引用您想要引用单元格和表格的方式:https://mega.nz/#!ZoZCgTaA!7gvkRw4pwecMfDXrNW_7jR2dKe2UR9jPsq9tp_CRIcU


在Storyboard中使用自动布局将非常简单:

在这里输入图像描述

label.numberOfLines = 0;

这可以通过自动布局轻松完成。 确保您的文字标签处于正确的视图中。

  • 确保您的文本标签的行数设置为0
  • 选择您的文本标签并固定顶部,左侧和右侧的限制。 点击底部的“添加3约束”按钮。
  • 在这里输入图像描述

  • 更新您的框架(查看故事板中的更新对齐)。
  • 在这里输入图像描述

    链接地址: http://www.djcxy.com/p/28191.html

    上一篇: Swift : align UILabel text in the top rather in the middle

    下一篇: aligning text of different sizes within a UILabel