Last active
September 22, 2022 20:08
-
-
Save iliaskarim/32af4fb9f70a3112d8602c66c15df033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import UIKit | |
struct CustomFont { | |
let filename: String | |
let fontName: String | |
func uiFontOfSize(_ size: CGFloat) -> UIFont { | |
UIFont(name: fontName, size: size)! | |
} | |
func register() { | |
let cfURL = Bundle.main.url(forResource: filename, withExtension: "ttf")! as CFURL | |
CTFontManagerRegisterFontsForURL(cfURL, CTFontManagerScope.process, nil) | |
} | |
} | |
protocol CustomFontFamily: CaseIterable { | |
static var name: String { get } | |
var filename: String { get } | |
var fontName: String { get } | |
} | |
extension CustomFontFamily { | |
static func register() { | |
Self.allCases.map(\.customFont).forEach { $0.register() } | |
} | |
private var customFont: CustomFont { | |
CustomFont(filename: filename, fontName: fontName) | |
} | |
func uiFontOfSize(_ size: CGFloat) -> UIFont { | |
customFont.uiFontOfSize(size) | |
} | |
} | |
enum FranklinGothic: CustomFontFamily { | |
static let name = "Franklin Gothic" | |
case condensed | |
case regular | |
var filename: String { | |
switch self { | |
case .condensed: return "Franklin Gothic Condensed" | |
case .regular: return "Franklin Gothic Regular" | |
} | |
} | |
var fontName: String { | |
switch self { | |
case .condensed: return "FranklinGothicCondensed" | |
case .regular: return "FranklinGothic" | |
} | |
} | |
} | |
enum UniversNextPro: CustomFontFamily { | |
static let name = "Univers Next Pro" | |
case condensed | |
case heavyCondensed | |
case thinCondensed | |
var filename: String { | |
switch self { | |
case .condensed: return "Univers Next Pro Condensed" | |
case .heavyCondensed: return "Univers Next Pro Heavy Condensed" | |
case .thinCondensed: return "Univers Next Pro Thin Condensed" | |
} | |
} | |
var fontName: String { | |
switch self { | |
case .condensed: return "UniversNextPro-Cond" | |
case .heavyCondensed: return "UniversNextPro-HeavyCond" | |
case .thinCondensed: return "UniversNextPro-ThinCond" | |
} | |
} | |
} | |
final class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
private let tableView = UITableView() | |
override func loadView() { | |
view = tableView | |
tableView.dataSource = self | |
tableView.delegate = self | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
FranklinGothic.register() | |
UniversNextPro.register() | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
2 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil) | |
cell.textLabel?.numberOfLines = 0 | |
cell.textLabel?.text = "The quick brown fox jumps over the lazy dog." | |
let font: UIFont | |
switch indexPath.section { | |
case 0: font = FranklinGothic.allCases[indexPath.row].uiFontOfSize(24.0) | |
case 1: font = UniversNextPro.allCases[indexPath.row].uiFontOfSize(24.0) | |
default: fatalError() | |
} | |
cell.textLabel?.font = font | |
let name: String | |
switch indexPath.section { | |
case 0: name = FranklinGothic.allCases[indexPath.row].fontName | |
case 1: name = UniversNextPro.allCases[indexPath.row].fontName | |
default: fatalError() | |
} | |
cell.detailTextLabel?.text = name | |
return cell | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
switch section { | |
case 0: return FranklinGothic.allCases.count | |
case 1: return UniversNextPro.allCases.count | |
default: fatalError() | |
} | |
} | |
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
switch section { | |
case 0: return FranklinGothic.name | |
case 1: return UniversNextPro.name | |
default: fatalError() | |
} | |
} | |
} | |
let viewController = TableViewController() | |
viewController.view.frame = CGRect(origin: .zero, size: CGSize(width: 320, height: 480)) | |
viewController.view.overrideUserInterfaceStyle = .dark | |
PlaygroundPage.current.liveView = viewController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment