Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stleamist committed Jun 5, 2022
1 parent 6c0401b commit 2b83846
Show file tree
Hide file tree
Showing 9 changed files with 514 additions and 32 deletions.
23 changes: 5 additions & 18 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.
// swift-tools-version:5.1

import PackageDescription

let package = Package(
name: "AttributedFont",
platforms: [.iOS(.v13), .macOS(.v10_15), .tvOS(.v13), .watchOS(.v6)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "AttributedFont",
targets: ["AttributedFont"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.library(name: "AttributedFont", targets: ["AttributedFont", "Previews"])
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "AttributedFont",
dependencies: []),
.testTarget(
name: "AttributedFontTests",
dependencies: ["AttributedFont"]),
.target(name: "AttributedFont", dependencies: []),
.target(name: "Previews", dependencies: ["AttributedFont"])
]
)
74 changes: 74 additions & 0 deletions Sources/AttributedFont/AttributedFont+Font.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import SwiftUI

// MARK: Creating Custom Fonts

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension AttributedFont {

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public static func custom(_ name: String, fixedSize: CGFloat, attributes: Attributes) -> Self {
return .init(name: name, fixedSize: fixedSize, attributes: attributes)
}
}

// MARK: Styling a Font

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension AttributedFont {

public func italic() -> Self {
var modified = self
modified.font = modified.font.italic()
return modified
}

public func smallCaps() -> Self {
var modified = self
modified.font = modified.font.smallCaps()
return modified
}

public func lowercaseSmallCaps() -> Self {
var modified = self
modified.font = modified.font.lowercaseSmallCaps()
return modified
}

public func uppercaseSmallCaps() -> Self {
var modified = self
modified.font = modified.font.uppercaseSmallCaps()
return modified
}

public func monospacedDigit() -> Self {
var modified = self
modified.font = modified.font.monospacedDigit()
return modified
}

public func weight(_ weight: Font.Weight) -> Self {
var modified = self
modified.font = modified.font.weight(weight)
return modified
}

public func bold() -> Self {
var modified = self
modified.font = modified.font.bold()
return modified
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public func monospaced() -> Self {
var modified = self
modified.font = modified.font.monospaced()
return modified
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public func leading(_ leading: Font.Leading) -> Self {
var modified = self
modified.font = modified.font.leading(leading)
return modified
}
}
57 changes: 54 additions & 3 deletions Sources/AttributedFont/AttributedFont.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
public struct AttributedFont {
public private(set) var text = "Hello, World!"
import SwiftUI

public init() {
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public struct AttributedFont: Hashable {

public internal(set) var name: String
public internal(set) var size: CGFloat
public internal(set) var attributes: Attributes

public internal(set) var font: Font
public internal(set) var ctFont: CTFont

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
internal init(name: String, fixedSize: CGFloat, attributes: Attributes) {
self.name = name
self.size = fixedSize
self.attributes = attributes

self.font = .custom(name, fixedSize: fixedSize)
self.ctFont = CTFontCreateWithName(name as CFString, size, nil)
}
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension AttributedFont {

public struct Attributes: Hashable {

public internal(set) var kerning: CGFloat?
public internal(set) var tracking: CGFloat?
public internal(set) var lineHeightMultiple: CGFloat?

public init(kerning: CGFloat? = nil, tracking: CGFloat? = nil, lineHeightMultiple: CGFloat? = nil) {
self.kerning = kerning
self.tracking = tracking
self.lineHeightMultiple = lineHeightMultiple
}
}
}


@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension AttributedFont {

public var lineSpacing: CGFloat? {
guard let lineHeightMultiple = attributes.lineHeightMultiple else {
return nil
}
let originalLineHeight = CTFontGetLineHeight(ctFont)
let customLineHeight = originalLineHeight * lineHeightMultiple
guard customLineHeight > originalLineHeight else {
return nil
}
let lineSpacing = customLineHeight - originalLineHeight
return lineSpacing
}
}
Loading

0 comments on commit 2b83846

Please sign in to comment.