美文网首页
QuartzCore - CATextLayer

QuartzCore - CATextLayer

作者: ienos | 来源:发表于2022-01-06 09:19 被阅读0次

    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()
    
        }
        
    }
    

    相关文章

      网友评论

          本文标题:QuartzCore - CATextLayer

          本文链接:https://www.haomeiwen.com/subject/qjplcrtx.html