【发布时间】:2015-08-26 01:41:57
【问题描述】:
我正在尝试创建一个消息传递应用程序,但遇到了一个非常奇怪的问题。
“Thomas”和文本气泡底部之间有这么多空间的原因是因为UILabel 正在创建另一行。目前我正在使用attributedText 属性设置标签的文本,并传入NSMutableParagraphStyle 和line spacing 的8。如果我将line spacing 设置为0,“Thomas”和文本气泡底部之间的空间会像这样消失:
这就是它变得奇怪的地方。如果我将段落line spacing 设置回8,并在该行中添加更多字符,则会出现没有多余行的文本气泡:
非常感谢所有帮助:)
这是我的代码:
class MessageTableViewCell: UITableViewCell {
var didSetupConstraints = false
var thumbnail = UIImageView.newAutoLayoutView()
let messageTailIcon = UIImageView.newAutoLayoutView()
var messageView = UIView.newAutoLayoutView()
var messageLabel = UILabel.newAutoLayoutView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
thumbnail.image = UIImage(named: "ThomasBaldwin")
thumbnail.layer.cornerRadius = 17.5
thumbnail.clipsToBounds = true
messageTailIcon.image = UIImage(named: "MessageTailIcon")
messageView.backgroundColor = Application.greyColor
messageView.layer.cornerRadius = 10
let paragraphStyle = NSMutableParagraphStyle.new()
paragraphStyle.lineSpacing = 8
messageLabel.numberOfLines = 0
messageLabel.layer.cornerRadius = 10
messageLabel.attributedText = NSMutableAttributedString(
string: "Thomas says hello",
attributes: [
NSFontAttributeName: UIFont(name: "AvenirNextLTPro-Regular", size: 12.5)!,
NSForegroundColorAttributeName: UIColor.colorFromCode(0x262626),
NSBackgroundColorAttributeName: Application.greyColor,
NSKernAttributeName: 0.5,
NSParagraphStyleAttributeName: paragraphStyle
]
)
contentView.addSubview(thumbnail)
contentView.addSubview(messageView)
messageView.addSubview(messageTailIcon)
messageView.addSubview(messageLabel)
updateConstraints()
}
override func updateConstraints() {
if !didSetupConstraints {
thumbnail.autoPinEdgeToSuperviewEdge(.Top, withInset: 15)
thumbnail.autoPinEdgeToSuperviewEdge(.Leading, withInset: 8.5)
thumbnail.autoSetDimensionsToSize(CGSize(width: 35, height: 35))
messageView.autoPinEdgeToSuperviewEdge(.Top, withInset: 17.5)
messageView.autoPinEdge(.Leading, toEdge: .Trailing, ofView: thumbnail, withOffset: 10)
messageView.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 24.5)
messageView.autoPinEdgeToSuperviewEdge(.Bottom)
messageTailIcon.autoPinEdgeToSuperviewEdge(.Top, withInset: 15)
messageTailIcon.autoPinEdgeToSuperviewEdge(.Leading, withInset: -10)
messageTailIcon.autoSetDimensionsToSize(CGSize(width: 18, height: 9))
messageLabel.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 8.5, left: 10, bottom: 8.5, right: 5), excludingEdge: .Trailing)
messageLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 0, relation: .GreaterThanOrEqual)
didSetupConstraints = true
}
super.updateConstraints()
}
}
如果您想查看演示该问题的示例项目,我已将其推送至github
【问题讨论】:
-
我在想是因为单元格被重用了,所以可能是由其他项目(单元格)属性引起的。你只有一个细胞吗?或表格视图中的多个单元格?可以分享大一点的屏幕吗?
-
@thomas,我认为您的 UIEdgeInsets 可能会导致您出现问题。由于您使用的自动布局库,很难告诉您可能是什么问题。很难发现这是您的代码有问题还是布局库的错误。
-
@Larcerax 我将一个演示该问题的示例项目推送到了 github,因此你们可以看到完整的代码:github.com/thomasbaldwin/SampleMessage
-
@Vigor 我将一个演示该问题的示例项目推送到 github,以便你们可以看到完整的代码
-
谢谢,让我看看这个,我只以编程方式做,看起来你也在做同样的事情,如果我看到任何东西,我会回复