-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52162e4
commit f842f99
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
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
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) | ||
} | ||
} |