问题描述
我希望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()
}
这里是指向示例项目的链接,以防万一您想参考单元格和表格的设置方式:>://://.mega.nz/#! I want the In your custom UITableViewCell class add this: 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 这篇关于Swift:将UILabel文本在顶部而不是在中间对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!UILabel
to start from the top even if the text is short it seems thatNSTextAlignment
doesn't workcell.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()
}
override func layoutSubviews() {
super.layoutSubviews()
textContent.sizeToFit()
}