From fb65423c70f3db23a6a1b93750f0adf557404c70 Mon Sep 17 00:00:00 2001 From: Tyson Freeze Date: Tue, 4 Jan 2022 22:44:55 -0700 Subject: [PATCH] Added a FittableLabelRoot class --- Source/FittableLabelRoot.swift | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Source/FittableLabelRoot.swift diff --git a/Source/FittableLabelRoot.swift b/Source/FittableLabelRoot.swift new file mode 100644 index 0000000..d9add2e --- /dev/null +++ b/Source/FittableLabelRoot.swift @@ -0,0 +1,69 @@ +// +// FittableLabelRoot.swift +// +// +// Created by Tyson_Freeze on 12/31/21. +// + +import UIKit + +/// A UIView subclass to give the FittableFontLabel package a root to recursively search for UILabels within. +open class FittableLabelRoot: UIView { + + /// If true, this view and it's subviews will be searched for any UILabels with a fontSizeLinkIdentifier. + @IBInspectable public var searchView: Bool = true + + // MARK: Private + + private var fontSizeLinks: [String : fontSizeLink] = [:] + + private class fontSizeLink { + var minimumFontSize: CGFloat = CGFloat.greatestFiniteMagnitude + var labels: [FittableFontLabel] = [] + + init(label: FittableFontLabel) { + add(label: label) + } + + func add(label: FittableFontLabel) { + labels.append(label) + if label.autoAdjustFontSize { + minimumFontSize = min(minimumFontSize, label.font.pointSize) + } + } + } + + // MARK: Life cycle + + open override func draw(_ rect: CGRect) { + updateFontSizeLinks(in: self) + standardizeFontSizes() + } + + // MARK: Search + + private func updateFontSizeLinks(in view: UIView) { + for subview in view.subviews { + print(subview.self) + if let label = subview as? FittableFontLabel { + if let fontSizeLinkIdentifier = label.fontSizeLinkIdentifier { + if let fontSizeLink = fontSizeLinks[fontSizeLinkIdentifier] { + fontSizeLink.add(label: label) + } else { + fontSizeLinks[fontSizeLinkIdentifier] = fontSizeLink(label: label) + } + } + } else { + updateFontSizeLinks(in: subview) + } + } + } + + private func standardizeFontSizes() { + for fontLink in fontSizeLinks.values { + for label in fontLink.labels { + label.font = label.font.withSize(fontLink.minimumFontSize) + } + } + } +}