常用于要求 label 文字上部与图片上部对齐的场景
import UIKit
enum VerticalAlignment
{
case VerticalAlignmentTop//default
case VerticalAlignmentMiddle
case VerticalAlignmentBottom
}
class VerticalLabel: UILabel
{
var verticalAlignment: VerticalAlignment = .VerticalAlignmentTop {
didSet {
self.setNeedsDisplay()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.verticalAlignment = VerticalAlignment.VerticalAlignmentTop
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
var superRect:CGRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
switch self.verticalAlignment {
case VerticalAlignment.VerticalAlignmentTop: