Initial Package
This commit is contained in:
parent
14cd9c6581
commit
e049a2cb60
|
@ -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"]),
|
||||
|
|
99
README.md
99
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)
|
||||
])
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -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<CFError>?
|
||||
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!"
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 146 KiB |
Loading…
Reference in New Issue