Skip to content

Commit dcf6ec9

Browse files
committed
Merge branch 'issue/allow_change_of_default_font' into issue/update_xcode_ios_version
2 parents 1994a24 + 7eddd37 commit dcf6ec9

5 files changed

Lines changed: 37 additions & 16 deletions

File tree

Aztec.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
FF24AC991F0146AF003CA91D /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF24AC981F0146AF003CA91D /* Assets.swift */; };
232232
FF437B6420D7CB2D000D9666 /* HTMLLi.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF437B6320D7CB2D000D9666 /* HTMLLi.swift */; };
233233
FF437B6620D7CB83000D9666 /* LiFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF437B6520D7CB83000D9666 /* LiFormatter.swift */; };
234+
FF48CAC8234DD5D3007FFC79 /* NSMutableAttributedString+ReplaceAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF48CAC7234DD5D3007FFC79 /* NSMutableAttributedString+ReplaceAttributes.swift */; };
234235
FF4E26601EA8DF1E005E8E42 /* ImageAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF4E265F1EA8DF1E005E8E42 /* ImageAttachment.swift */; };
235236
FF61909E202481F4004BCD0A /* CodeFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF61909D202481F4004BCD0A /* CodeFormatter.swift */; };
236237
FF7A1C511E5651EA00C4C7C8 /* LineAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7A1C501E5651EA00C4C7C8 /* LineAttachment.swift */; };
@@ -506,6 +507,7 @@
506507
FF24AC981F0146AF003CA91D /* Assets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assets.swift; sourceTree = "<group>"; };
507508
FF437B6320D7CB2D000D9666 /* HTMLLi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLLi.swift; sourceTree = "<group>"; };
508509
FF437B6520D7CB83000D9666 /* LiFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiFormatter.swift; sourceTree = "<group>"; };
510+
FF48CAC7234DD5D3007FFC79 /* NSMutableAttributedString+ReplaceAttributes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSMutableAttributedString+ReplaceAttributes.swift"; sourceTree = "<group>"; };
509511
FF4E265F1EA8DF1E005E8E42 /* ImageAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageAttachment.swift; sourceTree = "<group>"; };
510512
FF5B98E21DC29D0C00571CA4 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
511513
FF5B98E41DC355B400571CA4 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = SOURCE_ROOT; };
@@ -748,6 +750,7 @@
748750
FFD0FEB61DAE59A700430586 /* NSLayoutManager+Attachments.swift */,
749751
F1584795203C9B4C00EE05A1 /* NSMutableAttributedString+ParagraphProperty.swift */,
750752
F10BE6171EA7ADA6002E4625 /* NSMutableAttributedString+ReplaceOcurrences.swift */,
753+
FF48CAC7234DD5D3007FFC79 /* NSMutableAttributedString+ReplaceAttributes.swift */,
751754
F18733C41DA096EE005AEB80 /* NSRange+Helpers.swift */,
752755
FF20D6431EDC395E00294B78 /* NSTextingResult+Helpers.swift */,
753756
F1C05B9C1E37FA77007510EA /* String+CharacterName.swift */,
@@ -1643,6 +1646,7 @@
16431646
599F25381D8BC9A1002871D6 /* InAttributesConverter.swift in Sources */,
16441647
B551A4A01E770B3800EE3A7F /* UIFont+Emoji.swift in Sources */,
16451648
F13E8AD820B71BAA007C9F8A /* PluginManager.swift in Sources */,
1649+
FF48CAC8234DD5D3007FFC79 /* NSMutableAttributedString+ReplaceAttributes.swift in Sources */,
16461650
F12F586E1EF20394008AE298 /* LinkFormatter.swift in Sources */,
16471651
F1584796203C9B4C00EE05A1 /* NSMutableAttributedString+ParagraphProperty.swift in Sources */,
16481652
F15BA6092151501300424120 /* BoldStringAttributeConverter.swift in Sources */,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
import UIKit
3+
4+
public extension NSMutableAttributedString {
5+
6+
/// Replace all instances of the .font attribute that belong to the same family for the new font, trying to keep the same symbolic traits
7+
/// - Parameter font: the original font to be replaced
8+
/// - Parameter newFont: the new font to use.
9+
func replace(font: UIFont, with newFont: UIFont) {
10+
let fullRange = NSRange(location: 0, length: length)
11+
12+
beginEditing()
13+
enumerateAttributes(in: fullRange, options: []) { (attributes, subrange, stop) in
14+
guard let currentFont = attributes[.font] as? UIFont, currentFont.familyName == font.familyName else {
15+
return
16+
}
17+
var replacementFont = newFont
18+
if let fontDescriptor = newFont.fontDescriptor.withSymbolicTraits(currentFont.fontDescriptor.symbolicTraits) {
19+
replacementFont = UIFont(descriptor: fontDescriptor, size: currentFont.pointSize)
20+
}
21+
addAttribute(.font, value: replacementFont, range: subrange)
22+
}
23+
endEditing()
24+
}
25+
}

Aztec/Classes/TextKit/FontProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class FontProvider {
77

88
}
99

10-
static var shared = FontProvider()
10+
public static var shared = FontProvider()
1111

1212
public lazy var monospaceFont: UIFont = {
1313
let baseFont = UIFont(descriptor:UIFontDescriptor(name: "Menlo", size: 14), size:14)

Aztec/Classes/TextKit/LayoutManager.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ private extension LayoutManager {
317317
traits.remove(.traitBold)
318318
traits.remove(.traitItalic)
319319

320-
let descriptor = font.fontDescriptor.withSymbolicTraits(traits)
321-
return UIFont(descriptor: descriptor!, size: font.pointSize)
320+
if let descriptor = font.fontDescriptor.withSymbolicTraits(traits) {
321+
return UIFont(descriptor: descriptor, size: font.pointSize)
322+
} else {
323+
// Don't touch the font if we cannot remove the symbolic traits.
324+
return font
325+
}
322326
}
323327
}

Aztec/Classes/TextKit/TextView.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,10 @@ open class TextView: UITextView {
228228

229229
public var defaultFont: UIFont {
230230
didSet {
231-
refreshFont(oldFont: oldValue, newFont: defaultFont)
231+
textStorage.replace(font: oldValue, with: defaultFont)
232232
}
233233
}
234234

235-
private func refreshFont(oldFont: UIFont, newFont: UIFont) {
236-
let fullRange = NSRange(location: 0, length: textStorage.length)
237-
238-
textStorage.beginEditing()
239-
textStorage.enumerateAttributes(in: fullRange, options: []) { (attributes, subrange, stop) in
240-
if let currentFont = attributes[.font] as? UIFont, currentFont == oldFont {
241-
textStorage.addAttribute(.font, value: newFont, range: subrange)
242-
}
243-
}
244-
textStorage.endEditing()
245-
}
246-
247235
public let defaultParagraphStyle: ParagraphStyle
248236
var defaultMissingImage: UIImage
249237

0 commit comments

Comments
 (0)