Skip to content

Instantly share code, notes, and snippets.

@edudnyk
Created July 28, 2019 17:01
Show Gist options
  • Save edudnyk/0150f6f0f6b8d2c2d5381e016ca45b58 to your computer and use it in GitHub Desktop.
Save edudnyk/0150f6f0f6b8d2c2d5381e016ca45b58 to your computer and use it in GitHub Desktop.
Initial label layer implementation
import QuartzCore
import UIKit
open class LKLabelLayer : CALayer {
fileprivate var stringDrawingContext : NSStringDrawingContext!
fileprivate var stringDrawingOptions : NSStringDrawingOptions!
@objc open var attributedText : NSAttributedString?
public override init() {
super.init()
commonInit()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
stringDrawingOptions = [.usesFontLeading,
.usesLineFragmentOrigin,
.truncatesLastVisibleLine ]
stringDrawingContext = NSStringDrawingContext()
needsDisplayOnBoundsChange = true
isOpaque = false
}
open override func draw(in ctx: CGContext) {
drawText(in: ctx.boundingBoxOfClipPath)
}
fileprivate func drawText(in rect: CGRect) {
attributedText?.draw(with: rect,
options: stringDrawingOptions,
context: stringDrawingContext)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment