Skip to content

Commit

Permalink
Add UIFont
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Dec 15, 2024
1 parent 52162e4 commit f842f99
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Sources/Silica/UIKit/UIFont.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// UIFont.swift
// Cacao
//
// Created by Alsey Coleman Miller on 5/31/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

import struct Foundation.CGFloat

public final class UIFont {

// MARK: - Properties

public let cgFont: CGFont

// MARK: Font Name Attributes

public var fontName: String { return cgFont.name }

public var familyName: String { return cgFont.family }

// MARK: Font Metrics

public let pointSize: CGFloat

public lazy var descender: CGFloat = (CGFloat(self.cgFont.scaledFont.descent) * self.pointSize) / CGFloat(self.cgFont.scaledFont.unitsPerEm)

public lazy var ascender: CGFloat = (CGFloat(self.cgFont.scaledFont.ascent) * self.pointSize) / CGFloat(self.cgFont.scaledFont.unitsPerEm)

// MARK: - Initialization

public init?(name: String, size: CGFloat) {

guard let cgFont = CGFont(name: name)
else { return nil }

self.cgFont = cgFont
self.pointSize = size
}
}

// MARK: - Extensions

public extension UIFont {

static func systemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: "HelveticaNeu", size: size)!
}

static func boldSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: "HelveticaNeu-Bold", size: size)!
}
}

// MARK: - Equatable

extension UIFont: Equatable {

public static func == (lhs: UIFont, rhs: UIFont) -> Bool {
return lhs.fontName == rhs.fontName
&& lhs.pointSize == rhs.pointSize
}
}

// MARK: - Hashable

extension UIFont: Hashable {

public func hash(into hasher: inout Hasher) {
hasher.combine(fontName)
hasher.combine(pointSize)
}
}

0 comments on commit f842f99

Please sign in to comment.