diff --git a/Sources/YMatterType/Typography/FontInfo.swift b/Sources/YMatterType/Typography/FontInfo.swift index de071f7..1a6cf12 100644 --- a/Sources/YMatterType/Typography/FontInfo.swift +++ b/Sources/YMatterType/Typography/FontInfo.swift @@ -10,29 +10,30 @@ import UIKit /// Information about a font family. Default implementation of FontRepresentable. public struct FontInfo: FontRepresentable { - /// Suffix to use for Italic family font names "Italic" + /// Suffix to use for italic family font names "Italic" public static let italicSuffix = "Italic" /// Font family root name, e.g. "AvenirNext" public let familyName: String - /// Whether this is an Italic font - public let isItalic: Bool + /// Font style, e.g. regular or italic + public let style: Typography.FontStyle /// Initialize a `FontInfo` object /// - Parameters: /// - familyName: font family name - /// - isItalic: whether this font is Italic - public init(familyName: String, isItalic: Bool = false) { + /// - style: font style (default = `.regular`) + public init(familyName: String, style: Typography.FontStyle = .regular) { self.familyName = familyName - self.isItalic = isItalic + self.style = style } - /// Optional suffix to use for Italic version of the font. + /// Optional suffix to use for the font name. + /// /// Used by `FontRepresentable.fontName(for:compatibleWith:)` - /// e.g. "Italic" is a typical suffix for Italic fonts. + /// e.g. "Italic" is a typical suffix for italic fonts. /// default = "" public var fontNameSuffix: String { - isItalic ? FontInfo.italicSuffix : "" + (style == .italic) ? FontInfo.italicSuffix : "" } } diff --git a/Sources/YMatterType/Typography/FontRepresentable.swift b/Sources/YMatterType/Typography/FontRepresentable.swift index 27ff31e..c4e27d3 100644 --- a/Sources/YMatterType/Typography/FontRepresentable.swift +++ b/Sources/YMatterType/Typography/FontRepresentable.swift @@ -15,9 +15,10 @@ public protocol FontRepresentable { /// Font family root name, e.g. "AvenirNext" var familyName: String { get } - /// Optional suffix to use for Italic version of the font. + /// Optional suffix to use for the font name. + /// /// Used by `FontRepresentable.fontName(for:compatibleWith:)` - /// e.g. "Italic" is a typical suffix for Italic fonts. + /// e.g. "Italic" is a typical suffix for italic fonts. /// default = "" var fontNameSuffix: String { get } diff --git a/Sources/YMatterType/Typography/Typography+Enums.swift b/Sources/YMatterType/Typography/Typography+Enums.swift index 2a4f90b..d81f819 100644 --- a/Sources/YMatterType/Typography/Typography+Enums.swift +++ b/Sources/YMatterType/Typography/Typography+Enums.swift @@ -8,6 +8,14 @@ import UIKit extension Typography { + /// Font style (used together with font family name and font weight to load a specific font) + public enum FontStyle: String, CaseIterable { + /// Regular + case regular = "normal" + /// Italic + case italic + } + /// The nine basic font weights. Not all fonts support all 9 weights. public enum FontWeight: CGFloat, CaseIterable { /// ultralight (aka extra light) weight (100) diff --git a/Sources/YMatterType/Typography/Typography+SFPro.swift b/Sources/YMatterType/Typography/Typography+SFPro.swift index 1aeab81..9fed487 100644 --- a/Sources/YMatterType/Typography/Typography+SFPro.swift +++ b/Sources/YMatterType/Typography/Typography+SFPro.swift @@ -27,12 +27,16 @@ public struct SFProFontFamily: FontRepresentable { } } - /// Whether the font family is italic or not - public var isItalic: Bool { + /// Font style, e.g. regular or italic + public var style: Typography.FontStyle { + let style: Typography.FontStyle switch family { - case .displayItalic, .textItalic: return true - case .display, .text: return false + case .display, .text: + style = .regular + case .displayItalic, .textItalic: + style = .italic } + return style } fileprivate init(family: SFProFamily) { @@ -66,12 +70,13 @@ public struct SFProFontFamily: FontRepresentable { } } - /// Optional suffix to use for Italic version of the font. + /// Optional suffix to use for the font name. + /// /// Used by `FontRepresentable.fontName(for:compatibleWith:)` - /// e.g. "Italic" is a typical suffix for Italic fonts. + /// e.g. "Italic" is a typical suffix for italic fonts. /// default = "" public var fontNameSuffix: String { - isItalic ? FontInfo.italicSuffix : "" + (style == .italic) ? FontInfo.italicSuffix : "" } } diff --git a/Sources/YMatterType/Typography/Typography.swift b/Sources/YMatterType/Typography/Typography.swift index 2402b57..5de07c0 100644 --- a/Sources/YMatterType/Typography/Typography.swift +++ b/Sources/YMatterType/Typography/Typography.swift @@ -66,7 +66,7 @@ public struct Typography { /// Initializes a typography instance with the specified parameters /// - Parameters: /// - familyName: font family name - /// - isItalic: whether this font is italic or not (defaults to `.false`) + /// - fontStyle: font style (defaults to `regular`) /// - fontWeight: font weight to use /// - fontSize: font size to use /// - lineHeight: line height to use @@ -77,7 +77,7 @@ public struct Typography { /// - isFixed: `true` if this font should never scale, `false` if it should scale (defaults to `.false`) public init( familyName: String, - isItalic: Bool = false, + fontStyle: FontStyle = .regular, fontWeight: FontWeight, fontSize: CGFloat, lineHeight: CGFloat, @@ -88,7 +88,7 @@ public struct Typography { isFixed: Bool = false ) { self.init( - fontFamily: FontInfo(familyName: familyName, isItalic: isItalic), + fontFamily: FontInfo(familyName: familyName, style: fontStyle), fontWeight: fontWeight, fontSize: fontSize, lineHeight: lineHeight, diff --git a/Tests/YMatterTypeTests/Typography/Typography+FontTests.swift b/Tests/YMatterTypeTests/Typography/Typography+FontTests.swift index d1babdf..b574fde 100644 --- a/Tests/YMatterTypeTests/Typography/Typography+FontTests.swift +++ b/Tests/YMatterTypeTests/Typography/Typography+FontTests.swift @@ -44,7 +44,7 @@ final class TypographyFontTests: XCTestCase { } func testParagraphStyle() { - let fontInfo = FontInfo(familyName: "HelveticaNeue", isItalic: true) + let fontInfo = FontInfo(familyName: "HelveticaNeue", style: .italic) sizes.forEach { let typography = Typography( diff --git a/Tests/YMatterTypeTests/Typography/Typography+SFProTests.swift b/Tests/YMatterTypeTests/Typography/Typography+SFProTests.swift index ad9322a..9d013cc 100644 --- a/Tests/YMatterTypeTests/Typography/Typography+SFProTests.swift +++ b/Tests/YMatterTypeTests/Typography/Typography+SFProTests.swift @@ -27,24 +27,24 @@ final class TypographySFProTests: XCTestCase { } func testSFProDisplay() { - _testFontFamily(Typography.sfProDisplay) + _testFontFamily(Typography.sfProDisplay, style: .regular) } func testSFProDisplayItalic() { - _testFontFamily(Typography.sfProDisplayItalic, isItalic: true) + _testFontFamily(Typography.sfProDisplayItalic, style: .italic) } func testSFProText() { - _testFontFamily(Typography.sfProText) + _testFontFamily(Typography.sfProText, style: .regular) } func testSFProTextItalic() { - _testFontFamily(Typography.sfProTextItalic, isItalic: true) + _testFontFamily(Typography.sfProTextItalic, style: .italic) } } private extension TypographySFProTests { - func _testFontFamily(_ fontFamily: FontRepresentable, isItalic: Bool = false) { + func _testFontFamily(_ fontFamily: FontRepresentable, style: Typography.FontStyle) { Typography.FontWeight.allCases.forEach { let typography = Typography( fontFamily: fontFamily, @@ -54,12 +54,12 @@ private extension TypographySFProTests { textStyle: .callout ) - _testTypography(typography, isItalic: isItalic, traits: nil) - _testTypography(typography, isItalic: isItalic, traits: boldTraits) + _testTypography(typography, style: style, traits: nil) + _testTypography(typography, style: style, traits: boldTraits) } } - func _testTypography(_ typography: Typography, isItalic: Bool, traits: UITraitCollection?) { + func _testTypography(_ typography: Typography, style: Typography.FontStyle, traits: UITraitCollection?) { let layout = typography.generateLayout(compatibleWith: traits) // we expect a font @@ -73,7 +73,7 @@ private extension TypographySFProTests { // we expect the font to be italic or not let fontName = layout.font.fontName - if isItalic { + if style == .italic { XCTAssertTrue(fontName.hasSuffix(FontInfo.italicSuffix)) } else { XCTAssertFalse(fontName.hasSuffix(FontInfo.italicSuffix))