Skip to content

Commit

Permalink
Updated README, renamed identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Minimistro32 committed Jan 5, 2022
1 parent 4d4c9af commit 63f6cdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ But it won't always work as expected:
- Doesn't fit the label height
- Big top / bottom margins when the maxFontSize is huge
- Not really customisable
- Can't keep font size consistent across multiple labels
- ...

That's why `FittableFontLabel` exists:
Expand All @@ -28,6 +29,7 @@ That's why `FittableFontLabel` exists:
- Supports attributed string (custom line spacing...)
- Customize `maxFontSize` without using default label font size
- Auto-layout compliant
- Keep font size consistent across multiple labels using 'FittableRootView'
- `UILabel` extension if we want to use `UILabel`
- Customisable from xibs / storyboards when using the UILabel's subclass `FittableFontLabel`
- ...
Expand All @@ -44,8 +46,13 @@ That's why `FittableFontLabel` exists:

![](./assets/demo_single_line.gif)

**Consistent font size across multiple labels `FittableRootView`**

![](./assets/visual_fittable_root_view.png)

## Usage

### FittableFontLabel
```swift
let aFittableFontLabel = FittableFontLabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
aFittableFontLabel.autoFittableFont = true
Expand All @@ -59,6 +66,16 @@ Check the sample project for advanced usage.

**Note:** The label `lineBreakMode` must be set to `NSLineBreakByWordWrapping` in order to work.

### FittableRootView
To get a consistent font size across multiple labels embed your FittableFontLabels in a `UIView` with the custom class `FittableRootView`. Then give each label you want to keep consistent the same link identifer.

The FittableRootView acts as the root of a search for FittableFontLabels with link identifiers. Every FittableFontLabel with an identifier found by the search is updated to use the smallest auto adjusted font size calculated for that identifier.

**Notes:**
- FittableRootView has an inspectable bool `searchView`. This allows you to disable the search, giving the `FittableRootView` identical behavior to a normal `UIView`.
- You can use multiple identifiers to standardize the font size across multiple sets of labels within the same view.
- When AutoAdjustFontSize is false, that label's font size is ignored in the search for smallest font size. This way if you know which label is going to be longest (i.e. the smallest font size) you can avoid computing font sizes that will go unused while still keeping font size consistent.

## Installation

- iOS 8.0+
Expand All @@ -76,6 +93,7 @@ Add `github "tbaranes/FittableFontLabel"` to your Cartfile.
FittableFontLabel is available on SPM. Just add the following to your Package file:

```swift
// swift-tools-version:5.5.0
import PackageDescription

let package = Package(
Expand Down Expand Up @@ -148,7 +166,7 @@ The scale factor that determines the smallest font size to use during drawing. T
@IBInspectable public var bottomInset: CGFloat = 0
```

These four properties allow you to set a marge in your label. That will change the rect where the font must fit. The default value is 0.
These four properties allow you to set a margin on your label. That will change the rect where the font must fit. The default value is 0.

## Contribution

Expand Down
2 changes: 1 addition & 1 deletion Source/FittableFontLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class FittableFontLabel: UILabel {
@IBInspectable public var bottomInset: CGFloat = 0

/// Identifier
@IBInspectable public var fontSizeLinkIdentifier: String?
@IBInspectable public var linkIdentifier: String?

// MARK: Properties override

Expand Down
8 changes: 5 additions & 3 deletions Source/FittableRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ open class FittableRootView: UIView {
// MARK: Life cycle

open override func draw(_ rect: CGRect) {
updateFontSizeLinks(in: self)
standardizeFontSizes()
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.fontSizeLinkIdentifier {
if let fontSizeLinkIdentifier = label.linkIdentifier {
if let fontSizeLink = fontSizeLinks[fontSizeLinkIdentifier] {
fontSizeLink.add(label: label)
} else {
Expand Down
Binary file added assets/visual_fittable_root_view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 63f6cdc

Please sign in to comment.