Modify font descriptor traits when applying bold/italic formatting

instead of replacing font entirely.

Fixes #51
This commit is contained in:
Shadowfacts 2019-09-22 19:30:15 -04:00
parent 4b70b9d8b6
commit c84b042c33
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 29 additions and 2 deletions

View File

@ -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 = "<group>"; };
D667E5F42135BCD50057A976 /* ConversationTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationTableViewController.swift; sourceTree = "<group>"; };
D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mastodon+Equatable.swift"; sourceTree = "<group>"; };
D66A77BA233838DC0058F1EC /* UIFont+Traits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Traits.swift"; sourceTree = "<group>"; };
D6757A7B2157E01900721E32 /* XCBManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBManager.swift; sourceTree = "<group>"; };
D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBRequestSpec.swift; sourceTree = "<group>"; };
D6757A812157E8FA00721E32 /* XCBSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBSession.swift; sourceTree = "<group>"; };
@ -893,6 +895,7 @@
D6333B782139AEFD00CE884A /* Date+TimeAgo.swift */,
D67C57AE21E28EAD00C3118B /* Array+Uniques.swift */,
0450531E22B0097E00100BA2 /* Timline+UI.swift */,
D66A77BA233838DC0058F1EC /* UIFont+Traits.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -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 */,

View File

@ -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)
}
}

View File

@ -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":