【发布时间】:2015-06-26 00:19:28
【问题描述】:
我正在制作一个应用程序,其中我需要一个非常大的 UILabel 中的文本出现在标签边界的底部。有没有办法做到这一点?
通过代码或故事板很好。
【问题讨论】:
标签: ios swift labels text-alignment
我正在制作一个应用程序,其中我需要一个非常大的 UILabel 中的文本出现在标签边界的底部。有没有办法做到这一点?
通过代码或故事板很好。
【问题讨论】:
标签: ios swift labels text-alignment
对于 Swift 3,这里是顶部对齐
@objc(TopAlignLabel)
class TopAlignLabel: UILabel {
override func drawText(in rect: CGRect) {
var targetRect = textRect(forBounds: rect, limitedToNumberOfLines: numberOfLines)
targetRect.origin.y = 0
super.drawText(in: targetRect)
}
}
对于底部对齐,请改用rect.size.height - targetRect.size.height。
【讨论】:
如果子类化适合你,你可以继承 UILabel 并覆盖 |drawTextInRect:|在实际绘制文本之前调整绘图区域(CGRect)的方法
- (void)drawTextInRect:(CGRect)rect{
CGRect targetRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
targetRect.origin.y = rect.size.height - targetRect.size.height;
[super drawTextInRect:targetRect];
}
【讨论】:
使用自动布局,这很容易做到。
UIView。 【讨论】: