Created
July 28, 2019 17:01
-
-
Save edudnyk/0150f6f0f6b8d2c2d5381e016ca45b58 to your computer and use it in GitHub Desktop.
Initial label layer implementation
This file contains hidden or 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
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