diff --git a/Package.swift b/Package.swift index 3030b1d..d73de60 100644 --- a/Package.swift +++ b/Package.swift @@ -5,6 +5,7 @@ import PackageDescription let package = Package( name: "CustomFontsPackage", + platforms: [.iOS(.v14)], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( @@ -20,7 +21,10 @@ let package = Package( // Targets can depend on other targets in this package, and on products in packages this package depends on. .target( name: "CustomFontsPackage", - dependencies: []), + dependencies: [], + exclude: ["Images"], + resources: [.process("Fonts")] + ), .testTarget( name: "CustomFontsPackageTests", dependencies: ["CustomFontsPackage"]), diff --git a/README.md b/README.md index a532c24..a6826ee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,100 @@ # CustomFontsPackage -A description of this package. +A Swift package that includes custom fonts. + +## Installation + +Add the SPM package to your Xcode project at `main` + +`https://github.com/tarasis/CustomFontsPackage.git` + +## Usage + +In your AppDelegate: + +`import CustomFontsPackage` + +Then in `didFinishLaunchingWithOptions` add `registerFonts()` + +``` +func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + + // This code checks that the files are included in the CustomFonts bundle + if let files = try? FileManager.default.contentsOfDirectory(atPath: CustomFonts.fontBundle.bundlePath ) { + for file in files { + print(file) + } + } + + // This registers the fonts + registerFonts() + + + // This code checks all the names of the fonts that have been installed. This is definitely worthwhile including the first time you install the fonts. + for family in UIFont.familyNames.sorted() { + let names = UIFont.fontNames(forFamilyName: family) + print("Family: \(family) Font names: \(names)") + } + + return true +} +``` + +>Note that the names of these fonts can differ greatly from the filenames. +>Use the above code in the AppDelegate to check the name of the fonts. +>It will produce a similar output to the image below. The strings inside the `[]` are the names of the fonts. +> These names are what you should use in your code. Quite often they have a suffix telling you whether they are **Bold** or *Italic* etc. + +![](Sources/CustomFonts/Images/FontNames.png) + +### SwiftUI + +Use `Font.custom(_:size:)` to use your font. +``` +struct ContentView: View { + var body: some View { + VStack(spacing: 20) { + Text("Hello San Francisco") + Text("Hello BeautifulPeoplePersonalUse").font(Font.custom("BeautifulPeoplePersonalUse", size: 16)) + Text("Hello VeganStylePersonalUse").font(Font.custom("VeganStylePersonalUse", size: 16)) + } + } +} +``` +### UIKit +``` +class ViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + let label = UILabel(frame: .zero) + label.text = "Hello San Francisco" + label.font = UIFont(name: "San Francisco", size: 16) + + let label1 = UILabel(frame: .zero) + label1.text = "Hello BeautifulPeoplePersonalUse" + label1.font = UIFont(name: "BeautifulPeoplePersonalUse", size: 16) + + let label2 = UILabel(frame: .zero) + label2.text = "Hello VeganStylePersonalUse" + label2.font = UIFont(name: "VeganStylePersonalUse", size: 16) + + let stack = UIStackView(frame: .zero) + stack.axis = .vertical + stack.alignment = .center + stack.translatesAutoresizingMaskIntoConstraints = false + + stack.addArrangedSubview(label) + stack.addArrangedSubview(label1) + stack.addArrangedSubview(label2) + + view.addSubview(stack) + + NSLayoutConstraint.activate([ + stack.centerYAnchor.constraint(equalTo: view.centerYAnchor), + stack.centerXAnchor.constraint(equalTo: view.centerXAnchor) + ]) + } +} +``` diff --git a/Sources/CustomFontsPackage/CustomFontsPackage.swift b/Sources/CustomFontsPackage/CustomFontsPackage.swift index 938f774..109c750 100644 --- a/Sources/CustomFontsPackage/CustomFontsPackage.swift +++ b/Sources/CustomFontsPackage/CustomFontsPackage.swift @@ -1,3 +1,39 @@ +import Foundation +import UIKit + +public let fontBundle = Bundle.module + +public func registerFonts() { + _ = UIFont.registerFont(bundle: .module, fontName: "BeautifulPeople-Regular", fontExtension: "ttf") + _ = UIFont.registerFont(bundle: .module, fontName: "VeganStyle-Regular", fontExtension: "ttf") +} + +extension UIFont { + static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) -> Bool { + + guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension) else { + fatalError("Couldn't find font \(fontName)") + } + + guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL) else { + fatalError("Couldn't load data from the font \(fontName)") + } + + guard let font = CGFont(fontDataProvider) else { + fatalError("Couldn't create font from data") + } + + var error: Unmanaged? + let success = CTFontManagerRegisterGraphicsFont(font, &error) + guard success else { + print("Error registering font: \(fontName). Maybe it was already registered.") + return false + } + + return true + } +} + struct CustomFontsPackage { var text = "Hello, World!" } diff --git a/Sources/CustomFontsPackage/Fonts/BeautifulPeople-Regular.ttf b/Sources/CustomFontsPackage/Fonts/BeautifulPeople-Regular.ttf new file mode 100644 index 0000000..8746c57 Binary files /dev/null and b/Sources/CustomFontsPackage/Fonts/BeautifulPeople-Regular.ttf differ diff --git a/Sources/CustomFontsPackage/Fonts/VeganStyle-Regular.ttf b/Sources/CustomFontsPackage/Fonts/VeganStyle-Regular.ttf new file mode 100644 index 0000000..9fdf5c3 Binary files /dev/null and b/Sources/CustomFontsPackage/Fonts/VeganStyle-Regular.ttf differ diff --git a/Sources/CustomFontsPackage/Images/FontNames.png b/Sources/CustomFontsPackage/Images/FontNames.png new file mode 100644 index 0000000..70e534c Binary files /dev/null and b/Sources/CustomFontsPackage/Images/FontNames.png differ