ios Swift:在顶部而不是中间对齐 UILabel 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31778316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift : align UILabel text in the top rather in the middle
提问by James Moriarty
I want the UILabel
to start from the top even if the text is short it seems that
NSTextAlignment
doesn't work
我希望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()
}
回答by isaced
回答by Caleb
In your custom UITableViewCell class add this:
在您的自定义 UITableViewCell 类中添加:
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
这是示例项目的链接,以防您想参考单元格和表格的设置方式:https://mega.nz/#!ZoZCgTaA!7gvkRw4pwecMfDXrNW_7jR2dKe2UR9jPsq9tp_CRIcU
回答by David T
This can easily be done with auto layout. Ensure your text label is in the correct view.
这可以通过自动布局轻松完成。确保您的文本标签在正确的视图中。
- Ensure your text label's number of lines is set to 0
- 确保您的文本标签的行数设置为 0
- Select your text label and pin the top, left, and right constraints. Hit the 'Add 3 Constraints' button at the bottom.
- 选择您的文本标签并固定顶部、左侧和右侧的约束。点击底部的“添加 3 个约束”按钮。
- Update your frames (to see the updated alignment in storyboard).
- 更新您的框架(以查看故事板中更新的对齐方式)。
回答by Zeesha
In your custom UITableViewCell class add this:
在您的自定义 UITableViewCell 类中添加:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded() //This is the solution for :changed only after I tap the each cell
textContent.sizeToFit()
}