Skip to content

Commit

Permalink
Added UIFont.preferredFont(for style:weight:)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaddy committed Jan 3, 2021
1 parent b1030af commit a68a8d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions DJSwiftHelpers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
625380862594CE9E00C58B2F /* Double.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625380812594CE9B00C58B2F /* Double.swift */; };
6253808A2594CE9E00C58B2F /* Double.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625380812594CE9B00C58B2F /* Double.swift */; };
627631A7255D512F005C09B1 /* DJSwiftHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */; };
62C4485225A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
62C4485325A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
62C4485425A2208F0014F44B /* UIFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C4485125A2208F0014F44B /* UIFont.swift */; };
62D6CDE9255BFCCB00ABD216 /* Decodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D6CDE8255BFCCB00ABD216 /* Decodable.swift */; };
62D6CDEC255BFDA900ABD216 /* Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D6CDEB255BFDA900ABD216 /* Date.swift */; };
62D6CDEF255BFFA300ABD216 /* URLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D6CDEE255BFFA300ABD216 /* URLResponse.swift */; };
Expand Down Expand Up @@ -81,6 +84,7 @@
625380712594BCE900C58B2F /* CLLocationCoordinate2D.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLLocationCoordinate2D.swift; sourceTree = "<group>"; };
625380812594CE9B00C58B2F /* Double.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Double.swift; sourceTree = "<group>"; };
627631A6255D512F005C09B1 /* DJSwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DJSwiftHelpers.swift; sourceTree = "<group>"; };
62C4485125A2208F0014F44B /* UIFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIFont.swift; sourceTree = "<group>"; };
62D6CDE8255BFCCB00ABD216 /* Decodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decodable.swift; sourceTree = "<group>"; };
62D6CDEB255BFDA900ABD216 /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = "<group>"; };
62D6CDEE255BFFA300ABD216 /* URLResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLResponse.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -154,6 +158,7 @@
620F6A45250BBC1800B10032 /* UIApplication.swift */,
620F6A36250BB78200B10032 /* UIColor.swift */,
620F6A47250BBC2F00B10032 /* UIDevice.swift */,
62C4485125A2208F0014F44B /* UIFont.swift */,
620F6A49250BBC4B00B10032 /* UIImage.swift */,
62D6CDF4255C01F400ABD216 /* UILabel.swift */,
620F6A33250BB78200B10032 /* UIStackView.swift */,
Expand Down Expand Up @@ -327,6 +332,7 @@
620F6A38250BB78200B10032 /* UIStackView.swift in Sources */,
620F6A46250BBC1800B10032 /* UIApplication.swift in Sources */,
620F6A4E250BBCE900B10032 /* UserDefaults.swift in Sources */,
62C4485225A2208F0014F44B /* UIFont.swift in Sources */,
620F6A3B250BB78200B10032 /* UIColor.swift in Sources */,
625380822594CE9B00C58B2F /* Double.swift in Sources */,
62D6CDEF255BFFA300ABD216 /* URLResponse.swift in Sources */,
Expand All @@ -353,6 +359,7 @@
621B3C25255D467000A5E77F /* UIStackView.swift in Sources */,
621B3C26255D467000A5E77F /* UIApplication.swift in Sources */,
621B3C27255D467000A5E77F /* UserDefaults.swift in Sources */,
62C4485325A2208F0014F44B /* UIFont.swift in Sources */,
621B3C28255D467000A5E77F /* UIColor.swift in Sources */,
625380862594CE9E00C58B2F /* Double.swift in Sources */,
621B3C29255D467000A5E77F /* URLResponse.swift in Sources */,
Expand Down Expand Up @@ -380,6 +387,7 @@
62DF5081256E9A9000C88FF7 /* String.swift in Sources */,
62DF50A5256E9AA500C88FF7 /* URLRequest.swift in Sources */,
62DF50B5256E9AAD00C88FF7 /* Decodable.swift in Sources */,
62C4485425A2208F0014F44B /* UIFont.swift in Sources */,
62DF507C256E9A8B00C88FF7 /* Date.swift in Sources */,
6253808A2594CE9E00C58B2F /* Double.swift in Sources */,
62DF50A9256E9AA600C88FF7 /* URLResponse.swift in Sources */,
Expand Down
32 changes: 32 additions & 0 deletions DJSwiftHelpers/Helpers/UIFont.swift
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)
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ var isTV: Bool
var isCarPlay: Bool
```

### UIFont
Specify a font weight to a dynamic font style

```swift
static func preferredFont(for style: TextStyle, weight: Weight) -> UIFont
```
use like:

```swift
label.font = UIFont.preferredFont(for: .title2, weight: .medium)
```

### UIImage

Convert a String to Data then save it to the given URL path
Expand Down

0 comments on commit a68a8d1

Please sign in to comment.