CATextLayer 垂直居中显示,自定义 CATextLayer ,重写 draw(in ctx:) 方法
class CustomTextLayer: CATextLayer {
public override init() {
super.init()
}
public override init(layer: Any) {
super.init(layer: layer)
}
public required init(coder aDecoder: NSCoder) {
super.init(layer: aDecoder)
}
public override func draw(in ctx: CGContext) {
let mFontSize = (string as? NSAttributedString)?.size().height ?? fontSize
let yDiff = (bounds.size.height - mFontSize) * 0.5 + mFontSize * 0.1
ctx.saveGState()
ctx.translateBy(x: 0.0, y: yDiff)
super.draw(in: ctx)
ctx.restoreGState()
}
}
网友评论