Last active
December 19, 2022 08:09
-
-
Save AashirMubeeb/77bd375b0f032fdc233507b4e892391f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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