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

廉价感情. 提交于 2019-11-30 04:17:50

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

Using Auto Layout in Storyboard will be very simple:

and

label.numberOfLines = 0;

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).

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