vertically align text in a CATextLayer?

后端 未结 15 1400
时光说笑
时光说笑 2020-12-10 10:41

I am working on a CATextLayer that I want to use in both Mac and iOS. Can I control the vertical alignment of the text within the layer?

In this particular case, I

15条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 11:26

    Swift 5.3 for macOS

    class VerticallyAlignedTextLayer : CATextLayer {
        /* Credit - purebreadd - 6/24/2020
            https://stackoverflow.com/questions/4765461/vertically-align-text-in-a-catextlayer
         */
    
        func calculateMaxLines() -> Int {
            let maxSize = CGSize(width: frame.size.width, height: frame.size.width)
            let font = NSFont(descriptor: self.font!.fontDescriptor, size: self.fontSize)
            let charSize = floor(font!.capHeight)
            let text = (self.string ?? "") as! NSString
            let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font!], context: nil)
            let linesRoundedUp = Int(floor(textSize.height/charSize))
            return linesRoundedUp
        }
            
        override func draw(in context: CGContext) {
            let height = self.bounds.size.height
            let fontSize = self.fontSize
            let lines = CGFloat(calculateMaxLines())
            let yDiff = -(height - lines * fontSize) / 2 - lines * fontSize / 6.5 // Use -(height  - lines * fontSize) / 2 - lines * fontSize / 6.5 when in non-flipped coordinates (like macOS's default)
    
            context.saveGState()
            context.translateBy(x: 0, y: yDiff)
            super.draw(in: context)
            context.restoreGState()
        }
    }
    
    

    Notice I am dividing fontSize by 6.5, which seems to work better for my application of this solution. Thanks @purebreadd!

提交回复
热议问题