From dd6ca460280617a0a9fefa2f5141111dec82a63b Mon Sep 17 00:00:00 2001 From: Rob Napier Date: Sun, 5 Jan 2020 18:08:13 -0500 Subject: [PATCH] Support typographicBounds in demo --- PathText/Sources/PathText/LayoutManager.swift | 5 +++++ PathText/Sources/PathText/UIKit.swift | 5 +++++ PathTextDemo/ViewController.swift | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/PathText/Sources/PathText/LayoutManager.swift b/PathText/Sources/PathText/LayoutManager.swift index fc2c15e..a7aff30 100644 --- a/PathText/Sources/PathText/LayoutManager.swift +++ b/PathText/Sources/PathText/LayoutManager.swift @@ -22,6 +22,11 @@ struct PathTextLayoutManager { } } + public var typographicBounds: CGRect { + // FIXME: ensureLayout? Maybe pre-calculate this? + glyphRuns.reduce(.null) { $0.union($1.typographicBounds) } + } + mutating func ensureGlyphs() { if needsGlyphGeneration { updateGlyphRuns() } } diff --git a/PathText/Sources/PathText/UIKit.swift b/PathText/Sources/PathText/UIKit.swift index 685e3ae..878cedb 100644 --- a/PathText/Sources/PathText/UIKit.swift +++ b/PathText/Sources/PathText/UIKit.swift @@ -59,5 +59,10 @@ public class PathTextView: UIView { context.textMatrix = CGAffineTransform(scaleX: 1, y: -1) layoutManager.draw(in: context) } + + public var typographicBounds: CGRect { + layoutManager.ensureLayout() + return layoutManager.typographicBounds + } } #endif diff --git a/PathTextDemo/ViewController.swift b/PathTextDemo/ViewController.swift index 93f4b18..5340aab 100644 --- a/PathTextDemo/ViewController.swift +++ b/PathTextDemo/ViewController.swift @@ -53,7 +53,7 @@ class ViewController: UIViewController { var tightFrame = frame tightFrame.origin.y = 400 let tightTextView = PathTextView(frame: tightFrame, text: text, path: path) - tightTextView.sizeToFit() + tightTextView.bounds = tightTextView.typographicBounds tightTextView.layer.borderColor = UIColor.red.cgColor tightTextView.layer.borderWidth = 1 view.addSubview(tightTextView)