单行文本在UILabel中需要两行 [英] Single line text takes two lines in UILabel
问题描述
如您在图片中看到的,中间单元格有一个UILabel,它消耗两行,但是文本实际上是一行.看来,如果文本只需要几个字符即可创建新行,则iOS会假定它已经有2行.这很奇怪.
As you can see in the picture the middle cell has a UILabel that consumes two lines, but the text is actually a single line. It seems that if the text needs only few characters to create a new line, iOS assumes that it already has 2 lines. This is odd.
这是我创建标签的方式:
This is how I create the label:
self.titleLabel.lineBreakMode = .ByTruncatingTail
self.titleLabel.numberOfLines = 0
self.titleLabel.textAlignment = .Left
约束设置一次:
self.titleLabel.autoPinEdgeToSuperviewEdge(.Top)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Leading)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Trailing)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Bottom)
奇怪的是,当滚动表以使奇数单元格消失并再次向后滚动时,它具有其正常高度.滚动后:
The weird thing is, when the table is scrolled so that the odd cell disappears and scrolled back again it has its normal height. After scroll:
任何想法有什么问题吗?我正在使用swift,xcode6.1和iOS8.1
Any ideas whats wrong? I am using swift, xcode6.1 and iOS8.1
TableViewController:
TableViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CityTableViewCell.self, forCellReuseIdentifier:"cell")
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 52
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell: CityTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as? CityTableViewCell {
// Configure the cell for this indexPath
cell.updateFonts()
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
if indexPath.row == 1 {
cell.titleLabel.text = "Welcome to city 17, Mr. Gordon F."
} else {
cell.titleLabel.text = "Lamar!!!"
}
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
return UITableViewCell();
}
推荐答案
我认为您遇到了此错误: http://openradar.appspot.com/17799811 .标签未正确设置 preferredMaxLayoutWidth
.
I think you met this bug: http://openradar.appspot.com/17799811. The label does not set the preferredMaxLayoutWidth
correctly.
我选择的解决方法是用以下类将 UITableViewCell
子类化:
The workaround I chose was to subclass UITableViewCell
with the following class:
class VFTableViewCell : UITableViewCell {
@IBOutlet weak var testoLbl: UILabel!
//MARK: codice temporaneo per bug http://openradar.appspot.com/17799811
func maxWidth() -> CGFloat {
var appMax = CGRectGetWidth(UIApplication.sharedApplication().keyWindow.frame)
appMax -= 12 + 12 // borders, this is up to you (and should not be hardcoded here)
return appMax
}
override func awakeFromNib() {
super.awakeFromNib()
// MARK: Required for self-sizing cells.
self.testoLbl.preferredMaxLayoutWidth = maxWidth()
}
override func layoutSubviews() {
super.layoutSubviews()
// MARK: Required for self-sizing cells
self.testoLbl.preferredMaxLayoutWidth = maxWidth()
}
}
OP-Note:
OP-Note:
似乎自动布局无法正确计算UILabel布局的宽度.在我的UITableViewCell子类中将首选宽度设置为父级宽度可以解决我的问题:
It seems that auto layout does not calculate the width of the UILabel layout correctly. Setting the preferred width to the parents width in my UITableViewCell subclass solves my problem:
self.titleLabel.preferredMaxLayoutWidth = self.frame.width
在SO上找到: https://stackoverflow.com/a/19777242/401025
这篇关于单行文本在UILabel中需要两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!