Swift : 将 UILabel 文本对齐在顶部而不是在中间
Swift : align UILabel text in the top rather in the middle
我希望 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()
}
在 Storyboard 中使用自动布局会非常简单:
和
label.numberOfLines = 0;
在您的自定义 UITableViewCell class 中添加:
override func layoutSubviews() {
super.layoutSubviews()
textContent.sizeToFit()
}
这里有一个 link 示例项目,以防您想参考单元格和 table 的设置方式:
https://mega.nz/#!ZoZCgTaA!7gvkRw4pwecMfDXrNW_7jR2dKe2UR9jPsq9tp_CRIcU
这可以通过自动布局轻松完成。确保您的文本标签在正确的视图中。
- 确保文本标签的行数设置为 0
- Select 您的文本标签并固定顶部、左侧和右侧约束。点击底部的 'Add 3 Constraints' 按钮。
- 更新您的框架(以在故事板中查看更新后的对齐方式)。
在您的自定义 UITableViewCell class 中添加:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded() //This is the solution for :changed only after I tap the each cell
textContent.sizeToFit()
}
我希望 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()
}
在 Storyboard 中使用自动布局会非常简单:
和
label.numberOfLines = 0;
在您的自定义 UITableViewCell class 中添加:
override func layoutSubviews() {
super.layoutSubviews()
textContent.sizeToFit()
}
这里有一个 link 示例项目,以防您想参考单元格和 table 的设置方式: https://mega.nz/#!ZoZCgTaA!7gvkRw4pwecMfDXrNW_7jR2dKe2UR9jPsq9tp_CRIcU
这可以通过自动布局轻松完成。确保您的文本标签在正确的视图中。
- 确保文本标签的行数设置为 0
- Select 您的文本标签并固定顶部、左侧和右侧约束。点击底部的 'Add 3 Constraints' 按钮。
- 更新您的框架(以在故事板中查看更新后的对齐方式)。
在您的自定义 UITableViewCell class 中添加:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded() //This is the solution for :changed only after I tap the each cell
textContent.sizeToFit()
}