Giter Site home page Giter Site logo

customfonts's Introduction

CustomFonts

A Swift package that includes custom fonts.

Installation

Add the SPM package to your Xcode project at main

https://github.com/andrewmarmion/CustomFonts.git

Usage

In your AppDelegate:

import CustomFonts

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.

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)
        ])
    }
}

customfonts's People

Contributors

andrewmarmion avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.