Skip to content

Commit cdb2814

Browse files
committed
Use font provider class to provide default fonts
Instead of having random places where we obtain/configure fonts, this commits creates a central class where fonts can be retrieved.
1 parent efc0760 commit cdb2814

5 files changed

Lines changed: 42 additions & 27 deletions

File tree

Aztec.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
FF61909E202481F4004BCD0A /* CodeFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF61909D202481F4004BCD0A /* CodeFormatter.swift */; };
236236
FF7A1C511E5651EA00C4C7C8 /* LineAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7A1C501E5651EA00C4C7C8 /* LineAttachment.swift */; };
237237
FF7C89B01E3BC52F000472A8 /* NSAttributedString+FontTraits.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7C89AF1E3BC52F000472A8 /* NSAttributedString+FontTraits.swift */; };
238+
FF7EAEC4234D253B007A26E0 /* FontProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7EAEC3234D253B007A26E0 /* FontProvider.swift */; };
238239
FFA61E891DF18F3D00B71BF6 /* ParagraphStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */; };
239240
FFA61EC21DF6C1C900B71BF6 /* NSAttributedString+Archive.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */; };
240241
FFB5D29720BEB21A0038DCFB /* CiteFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */; };
@@ -513,6 +514,7 @@
513514
FF7A1C4A1E51F05700C4C7C8 /* CONTRIBUTING.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = "<group>"; };
514515
FF7A1C501E5651EA00C4C7C8 /* LineAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineAttachment.swift; sourceTree = "<group>"; };
515516
FF7C89AF1E3BC52F000472A8 /* NSAttributedString+FontTraits.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+FontTraits.swift"; sourceTree = "<group>"; };
517+
FF7EAEC3234D253B007A26E0 /* FontProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontProvider.swift; sourceTree = "<group>"; };
516518
FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParagraphStyle.swift; sourceTree = "<group>"; };
517519
FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+Archive.swift"; sourceTree = "<group>"; };
518520
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CiteFormatter.swift; sourceTree = "<group>"; };
@@ -804,6 +806,7 @@
804806
FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */,
805807
B5A99D831EBA073D00DED081 /* HTMLStorage.swift */,
806808
F9982CF521877663001E606B /* TextViewPasteboardDelegate.swift */,
809+
FF7EAEC3234D253B007A26E0 /* FontProvider.swift */,
807810
);
808811
path = TextKit;
809812
sourceTree = "<group>";
@@ -1573,6 +1576,7 @@
15731576
F165D92A20C72EF500EAA6B0 /* Array+ShortcodeAttribute.swift in Sources */,
15741577
F165D92420C72C1000EAA6B0 /* ShortcodeAttributeSerializer.swift in Sources */,
15751578
F177FF751FB6404D00CBBE35 /* UILayoutPriority+Swift4.swift in Sources */,
1579+
FF7EAEC4234D253B007A26E0 /* FontProvider.swift in Sources */,
15761580
F1C05B9D1E37FA77007510EA /* String+CharacterName.swift in Sources */,
15771581
F1FF7D9F201A1D24007B0B32 /* Figcaption.swift in Sources */,
15781582
FF17B2D2227A589F0022AECE /* LIElementConverter.swift in Sources */,

Aztec/Classes/Formatters/Implementations/CodeFormatter.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@ class CodeFormatter: AttributeFormatter {
1212

1313
// MARK: - Init
1414

15-
init(monospaceFont: UIFont = UIFont(descriptor:UIFontDescriptor(name: "Courier", size: 12), size:12), backgroundColor: UIColor = UIColor.lightGray) {
16-
let font: UIFont
17-
18-
if #available(iOS 11.0, *) {
19-
font = UIFontMetrics.default.scaledFont(for: monospaceFont)
20-
} else {
21-
font = monospaceFont
22-
}
23-
self.monospaceFont = font
15+
init(monospaceFont: UIFont = FontProvider.shared.monospaceFont, backgroundColor: UIColor = UIColor.lightGray) {
16+
self.monospaceFont = monospaceFont
2417
self.backgroundColor = backgroundColor
2518
self.htmlRepresentationKey = .codeHtmlRepresentation
2619
}

Aztec/Classes/Formatters/Implementations/PreFormatter.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,10 @@ open class PreFormatter: ParagraphAttributeFormatter {
1414
///
1515
let placeholderAttributes: [NSAttributedString.Key: Any]?
1616

17-
1817
/// Designated Initializer
1918
///
20-
init(monospaceFont: UIFont = UIFont(descriptor:UIFontDescriptor(name: "Courier", size: 12), size:12), placeholderAttributes: [NSAttributedString.Key : Any]? = nil) {
21-
let font: UIFont
22-
23-
if #available(iOS 11.0, *) {
24-
font = UIFontMetrics.default.scaledFont(for: monospaceFont)
25-
} else {
26-
font = monospaceFont
27-
}
28-
29-
self.monospaceFont = font
19+
init(monospaceFont: UIFont = FontProvider.shared.monospaceFont, placeholderAttributes: [NSAttributedString.Key : Any]? = nil) {
20+
self.monospaceFont = monospaceFont
3021
self.placeholderAttributes = placeholderAttributes
3122
}
3223

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Foundation
2+
import UIKit
3+
4+
public class FontProvider {
5+
6+
private init() {
7+
8+
}
9+
10+
static var shared = FontProvider()
11+
12+
lazy var monospaceFont: UIFont = {
13+
let baseFont = UIFont(descriptor:UIFontDescriptor(name: "Menlo", size: 14), size:14)
14+
let font: UIFont
15+
if #available(iOS 11.0, *) {
16+
font = UIFontMetrics.default.scaledFont(for: baseFont)
17+
} else {
18+
font = baseFont
19+
}
20+
return font
21+
}()
22+
23+
lazy var defaultFont: UIFont = {
24+
let baseFont = UIFont.systemFont(ofSize: 14)
25+
let font: UIFont
26+
if #available(iOS 11.0, *) {
27+
font = UIFontMetrics.default.scaledFont(for: baseFont)
28+
} else {
29+
font = baseFont
30+
}
31+
return font
32+
}()
33+
}

Aztec/Classes/TextKit/TextView.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,7 @@ open class TextView: UITextView {
397397
}
398398

399399
required public init?(coder aDecoder: NSCoder) {
400-
let font = UIFont.systemFont(ofSize: 14)
401-
if #available(iOS 11.0, *) {
402-
self.defaultFont = UIFontMetrics.default.scaledFont(for: font)
403-
} else {
404-
self.defaultFont = font
405-
}
406-
400+
self.defaultFont = FontProvider.shared.defaultFont
407401
defaultParagraphStyle = ParagraphStyle.default
408402
defaultMissingImage = Assets.imageIcon
409403

0 commit comments

Comments
 (0)