-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b64affc
commit fb65423
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |