From c84b042c33ec2a6b6c3ff6827b477298d1eeaaeb Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 22 Sep 2019 19:30:15 -0400 Subject: [PATCH] Modify font descriptor traits when applying bold/italic formatting instead of replacing font entirely. Fixes #51 --- Tusker.xcodeproj/project.pbxproj | 4 ++++ Tusker/Extensions/UIFont+Traits.swift | 21 +++++++++++++++++++++ Tusker/Views/ContentLabel.swift | 6 ++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 Tusker/Extensions/UIFont+Traits.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 9851b6f207..7f6da374c6 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -118,6 +118,7 @@ D667E5F12134D5050057A976 /* UIViewController+Delegates.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F02134D5050057A976 /* UIViewController+Delegates.swift */; }; D667E5F52135BCD50057A976 /* ConversationTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F42135BCD50057A976 /* ConversationTableViewController.swift */; }; D667E5F82135C3040057A976 /* Mastodon+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */; }; + D66A77BB233838DC0058F1EC /* UIFont+Traits.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66A77BA233838DC0058F1EC /* UIFont+Traits.swift */; }; D6757A7C2157E01900721E32 /* XCBManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7B2157E01900721E32 /* XCBManager.swift */; }; D6757A7E2157E02600721E32 /* XCBRequestSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */; }; D6757A822157E8FA00721E32 /* XCBSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A812157E8FA00721E32 /* XCBSession.swift */; }; @@ -376,6 +377,7 @@ D667E5F02134D5050057A976 /* UIViewController+Delegates.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Delegates.swift"; sourceTree = ""; }; D667E5F42135BCD50057A976 /* ConversationTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationTableViewController.swift; sourceTree = ""; }; D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mastodon+Equatable.swift"; sourceTree = ""; }; + D66A77BA233838DC0058F1EC /* UIFont+Traits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Traits.swift"; sourceTree = ""; }; D6757A7B2157E01900721E32 /* XCBManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBManager.swift; sourceTree = ""; }; D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBRequestSpec.swift; sourceTree = ""; }; D6757A812157E8FA00721E32 /* XCBSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBSession.swift; sourceTree = ""; }; @@ -893,6 +895,7 @@ D6333B782139AEFD00CE884A /* Date+TimeAgo.swift */, D67C57AE21E28EAD00C3118B /* Array+Uniques.swift */, 0450531E22B0097E00100BA2 /* Timline+UI.swift */, + D66A77BA233838DC0058F1EC /* UIFont+Traits.swift */, ); path = Extensions; sourceTree = ""; @@ -1559,6 +1562,7 @@ D6D58DF922074B74009C8DD9 /* LinkLabel.swift in Sources */, 0454DDAF22B462EF00B8BB8E /* GalleryExpandAnimationController.swift in Sources */, D6A3BC8A2321F79B00FD64D5 /* AccountTableViewCell.swift in Sources */, + D66A77BB233838DC0058F1EC /* UIFont+Traits.swift in Sources */, D68FEC4F232C5BC300C84F23 /* SegmentedPageViewController.swift in Sources */, 0450531F22B0097E00100BA2 /* Timline+UI.swift in Sources */, D667E5F52135BCD50057A976 /* ConversationTableViewController.swift in Sources */, diff --git a/Tusker/Extensions/UIFont+Traits.swift b/Tusker/Extensions/UIFont+Traits.swift new file mode 100644 index 0000000000..36d40b2f13 --- /dev/null +++ b/Tusker/Extensions/UIFont+Traits.swift @@ -0,0 +1,21 @@ +// +// UIFont+Traits.swift +// Tusker +// +// Created by Shadowfacts on 9/22/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import UIKit + +extension UIFont { + + func addingTraits(_ traits: UIFontDescriptor.SymbolicTraits, size: CGFloat? = nil) -> UIFont? { + let descriptor = self.fontDescriptor + guard let newDescriptor = descriptor.withSymbolicTraits([descriptor.symbolicTraits, traits]) else { + return nil + } + return UIFont(descriptor: newDescriptor, size: size ?? self.pointSize) + } + +} diff --git a/Tusker/Views/ContentLabel.swift b/Tusker/Views/ContentLabel.swift index dcd7b55d82..cd72008b63 100644 --- a/Tusker/Views/ContentLabel.swift +++ b/Tusker/Views/ContentLabel.swift @@ -128,9 +128,11 @@ class ContentLabel: LinkLabel { case "p": attributed.append(NSAttributedString(string: "\n\n")) case "em", "i": - attributed.addAttribute(.font, value: UIFont.italicSystemFont(ofSize: font!.pointSize), range: attributed.fullRange) + let currentFont: UIFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? self.font + attributed.addAttribute(.font, value: currentFont.addingTraits(.traitItalic), range: attributed.fullRange) case "strong", "b": - attributed.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: font!.pointSize), range: attributed.fullRange) + let currentFont: UIFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? self.font + attributed.addAttribute(.font, value: currentFont.addingTraits(.traitBold), range: attributed.fullRange) case "del": attributed.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: attributed.fullRange) case "code":