-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UIFont.preferredFont(for style:weight:)
- Loading branch information
Showing
3 changed files
with
52 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
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,32 @@ | ||
// | ||
// UIFont.swift | ||
// DJSwiftHelpers | ||
// | ||
// Created by Darren Jones on 03/01/2021. | ||
// Copyright © 2021 Dappological Ltd. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
@available(iOS 11.0, *) | ||
public | ||
extension UIFont { | ||
|
||
/** | ||
Allows you to specify a font weight to a dynamic font style | ||
- Parameters: | ||
- style: eg. `.title2` | ||
- weight: eg. `.medium` | ||
|
||
Example usage: | ||
``` | ||
label.font = UIFont.preferredFont(for: .title2, weight: .medium) | ||
``` | ||
*/ | ||
static func preferredFont(for style: TextStyle, weight: Weight) -> UIFont { | ||
let metrics = UIFontMetrics(forTextStyle: style) | ||
let desc = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style) | ||
let font = UIFont.systemFont(ofSize: desc.pointSize, weight: weight) | ||
return metrics.scaledFont(for: font) | ||
} | ||
} |
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