-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paywalls
: added support for custom fonts (#2988)
This adds a new `PaywallFontProvider` `protocol` as well as 2 implementations: - `DefaultPaywallFontProvider`: exactly equivalent to the existing font behavior - `CustomPaywallFontProvider` allows using a custom font name and still use dynamic type ![image](https://github.com/RevenueCat/purchases-ios/assets/685609/3659576c-a957-4f33-86ba-e3633520aa17) ![image](https://github.com/RevenueCat/purchases-ios/assets/685609/250304ae-c2e7-4726-888c-bb454651664e)
- Loading branch information
Showing
21 changed files
with
358 additions
and
63 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
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,102 @@ | ||
// | ||
// PaywallFontProvider.swift | ||
// | ||
// | ||
// Created by Nacho Soto on 8/8/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// A type that returns a font for a given `Font.TextStyle`. | ||
/// | ||
/// You can use one of the provided implementations, or make your own: | ||
/// - ``DefaultPaywallFontProvider`` | ||
/// - ``CustomPaywallFontProvider`` | ||
public protocol PaywallFontProvider { | ||
|
||
/// - Returns: Desired `Font` for the given `Font.TextStyle`. | ||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *) | ||
func font(for textStyle: Font.TextStyle) -> Font | ||
|
||
} | ||
|
||
/// Default ``PaywallFontProvider`` which uses the system default font | ||
/// supporting dynamic type. | ||
open class DefaultPaywallFontProvider: PaywallFontProvider { | ||
|
||
// swiftlint:disable:next missing_docs | ||
public init() {} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *) | ||
// swiftlint:disable:next cyclomatic_complexity missing_docs | ||
open func font(for textStyle: Font.TextStyle) -> Font { | ||
switch textStyle { | ||
case .largeTitle: return .largeTitle | ||
case .title: return .title | ||
case .title2: return .title2 | ||
case .title3: return .title3 | ||
case .headline: return .headline | ||
case .subheadline: return .subheadline | ||
case .body: return .body | ||
case .callout: return .callout | ||
case .footnote: return .footnote | ||
case .caption: return .caption | ||
case .caption2: return .caption2 | ||
@unknown default: return .body | ||
} | ||
} | ||
|
||
} | ||
|
||
#if canImport(UIKit) | ||
|
||
/// A ``PaywallFontProvider`` implementation that allows you to provide a custom | ||
/// font name, and it will automatically scale up based on the size category. | ||
open class CustomPaywallFontProvider: PaywallFontProvider { | ||
|
||
private let fontName: String | ||
|
||
/// Creates a ``CustomPaywallFontProvider`` with a name. | ||
public init(fontName: String) { | ||
self.fontName = fontName | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *) | ||
// swiftlint:disable:next missing_docs | ||
open func font(for textStyle: Font.TextStyle) -> Font { | ||
return Font.custom(self.fontName, | ||
size: UIFont.preferredFont(forTextStyle: textStyle.style).pointSize, | ||
relativeTo: textStyle) | ||
} | ||
|
||
} | ||
|
||
#endif | ||
|
||
// MARK: - Private | ||
|
||
#if canImport(UIKit) | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, *) | ||
private extension Font.TextStyle { | ||
|
||
var style: UIFont.TextStyle { | ||
switch self { | ||
case .largeTitle: return .largeTitle | ||
case .title: return .title1 | ||
case .title2: return .title2 | ||
case .title3: return .title3 | ||
case .headline: return .headline | ||
case .subheadline: return .subheadline | ||
case .body: return .body | ||
case .callout: return .callout | ||
case .footnote: return .footnote | ||
case .caption: return .caption1 | ||
case .caption2: return .caption2 | ||
@unknown default: return .body | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif |
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
Oops, something went wrong.