How the UILabel
可以从底部对齐。假设我的标签可以容纳三行文本。如果输入文本是单行,那么这一行应该位于标签的底部。请参考下图以更好地理解。橙色区域是标签的全框,目前只有一行,居中对齐。所以我想要的是,无论有多少行,它都应该始终对齐底部。
请提出您的想法。
谢谢。
Swift 4.2 版本使用contentMode
设置顶部和底部的属性:
class VerticalAlignedLabel: UILabel {
override func drawText(in rect: CGRect) {
var newRect = rect
switch contentMode {
case .top:
newRect.size.height = sizeThatFits(rect.size).height
case .bottom:
let height = sizeThatFits(rect.size).height
newRect.origin.y += rect.size.height - height
newRect.size.height = height
default:
()
}
super.drawText(in: newRect)
}
}
然后像这样设置你的标签:
let label = VerticalAlignedLabel()
label.contentMode = .bottom
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)