Skip to content

Instantly share code, notes, and snippets.

@AashirMubeeb
Last active December 19, 2022 08:09
Show Gist options
  • Save AashirMubeeb/77bd375b0f032fdc233507b4e892391f to your computer and use it in GitHub Desktop.
Save AashirMubeeb/77bd375b0f032fdc233507b4e892391f to your computer and use it in GitHub Desktop.
//
// Created by Muhammad Ashir Mubeen on 13/07/2022.
//
#if os(iOS) || os(tvOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif
public class CenterTextLayer: 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) {
#if os(iOS) || os(tvOS)
let multiplier = CGFloat(1)
#elseif os(OSX)
let multiplier = CGFloat(-1)
#endif
let yDiff = (bounds.size.height - ((string as? NSAttributedString)?.size().height ?? fontSize)) / 2 * multiplier
ctx.saveGState()
ctx.translateBy(x: 0.0, y: yDiff)
super.draw(in: ctx)
ctx.restoreGState()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment