-
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.
Merge pull request #38 from Minimistro32/master
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// swift-tools-version:5.5.0 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
|
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
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
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,70 @@ | ||
// | ||
// FittableRootView.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 FittableRootView: 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) { | ||
if searchView { | ||
updateFontSizeLinks(in: self) | ||
standardizeFontSizes() | ||
} | ||
} | ||
|
||
// MARK: Search | ||
|
||
private func updateFontSizeLinks(in view: UIView) { | ||
for subview in view.subviews { | ||
if let label = subview as? FittableFontLabel { | ||
if let fontSizeLinkIdentifier = label.linkIdentifier { | ||
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) | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.