常用于要求 label 文字上部与图片上部对齐的场景
import UIKitenum 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: superRect.origin.y = bounds.origin.y case VerticalAlignment.VerticalAlignmentBottom: superRect.origin.y = bounds.origin.y + bounds.size.height - superRect.size.height case VerticalAlignment.VerticalAlignmentMIDdle: superRect.origin.y = bounds.origin.y + (bounds.size.height - superRect.size.height) / 2 } return superRect } overrIDe func drawText(in rect: CGRect) { let rect:CGRect = self.textRect(forBounds: rect,limitedToNumberOflines: self.numberOflines) super.drawText(in: rect) }}总结
以上是内存溢出为你收集整理的swift 自定义一个可设置 text 的垂直位置的 Label全部内容,希望文章能够帮你解决swift 自定义一个可设置 text 的垂直位置的 Label所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)