From 6c3598efee2ad87dd0059083144d5fe88de874aa Mon Sep 17 00:00:00 2001 From: commiekong <30882689+dfsm@users.noreply.github.com> Date: Tue, 31 May 2022 22:36:23 -0300 Subject: [PATCH] Support rounded fonts in themes. --- App/Data Sources/MessageListDataSource.swift | 20 ++++++-- App/Data Sources/ThreadListDataSource.swift | 51 +++++++++++++++---- App/Extensions/UIFont+MonospacedDigits.swift | 9 ++++ App/Extensions/UIKit.swift | 12 ++++- App/Navigation/NavigationBar.swift | 11 +++- App/Navigation/NavigationController.swift | 31 +++++++---- App/Theming/Themes.plist | 2 + App/Theming/Themes.swift | 4 ++ .../Forums/ForumsTableViewController.swift | 16 +++++- .../Messages/MessageListViewController.swift | 8 +++ .../Posts/PostsPageTopBar.swift | 21 ++++++-- .../Threads/ThreadsTableViewController.swift | 6 +++ 12 files changed, 159 insertions(+), 32 deletions(-) mode change 100644 => 100755 App/Theming/Themes.plist diff --git a/App/Data Sources/MessageListDataSource.swift b/App/Data Sources/MessageListDataSource.swift index bc23e3818..8f450f8b9 100644 --- a/App/Data Sources/MessageListDataSource.swift +++ b/App/Data Sources/MessageListDataSource.swift @@ -99,22 +99,34 @@ extension MessageListDataSource: UITableViewDataSource { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "d MMM" + + var sentDateRawFont: UIFont + var senderFont: UIFont + var subjectFont: UIFont + if Theme.defaultTheme().roundedFonts { + sentDateRawFont = roundedFont(ofSize: 12, weight: .semibold) + senderFont = roundedFont(ofSize: 13, weight: .semibold) + subjectFont = roundedFont(ofSize: 17, weight: .regular) + } else { + sentDateRawFont = UIFont.systemFont(ofSize: 12) + senderFont = UIFont.systemFont(ofSize: 15, weight: .regular) + subjectFont = UIFont.systemFont(ofSize: 15, weight: .regular) + } return MessageListCell.ViewModel( backgroundColor: theme["listBackgroundColor"]!, selectedBackgroundColor: theme["listSelectedBackgroundColor"]!, sender: NSAttributedString(string: message.fromUsername ?? "", attributes: [ - .font: UIFont.boldSystemFont(ofSize: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.subheadline).pointSize), .foregroundColor: theme[color: "listTextColor"]!]), + .font: senderFont, sentDate: message.sentDate ?? .distantPast, sentDateAttributes: [ - .font: UIFont.preferredFontForTextStyle(.body, fontName: nil, sizeAdjustment: -2), .foregroundColor: theme[color: "listTextColor"]!], sentDateRaw: NSAttributedString(string: dateFormatter.string(from: message.sentDate!), attributes: [ - .font: UIFont.systemFont(ofSize: 12), + .font: sentDateRawFont, .foregroundColor: theme[color: "listSecondaryTextColor"]!]), subject: NSAttributedString(string: message.subject ?? "", attributes: [ - .font: UIFont.preferredFontForTextStyle(.body, fontName: nil, sizeAdjustment: -2), + .font: subjectFont, .foregroundColor: theme[color: "listTextColor"]!]), tagImage: .image(name: message.threadTag?.imageName, placeholder: .privateMessage), tagOverlayImage: { diff --git a/App/Data Sources/ThreadListDataSource.swift b/App/Data Sources/ThreadListDataSource.swift index fb8c48674..c702c9b23 100644 --- a/App/Data Sources/ThreadListDataSource.swift +++ b/App/Data Sources/ThreadListDataSource.swift @@ -148,9 +148,19 @@ extension ThreadListDataSource: UITableViewDataSource { return ThreadListCell.ViewModel( backgroundColor: theme["listBackgroundColor"]!, - pageCount: NSAttributedString(string: "\(thread.numberOfPages)", attributes: [ - .font: UIFont.preferredFontForTextStyle(.footnote, fontName: theme["listFontName"]), - .foregroundColor: theme[color: "listSecondaryTextColor"]!]), + pageCount: { + var pagecountFont: UIFont + if Theme.defaultTheme().roundedFonts { + pagecountFont = roundedFont(ofSize: 13, weight: .semibold) + } else { + pagecountFont = UIFont.systemFont(ofSize: 13, weight: .medium) + } + + return NSAttributedString(string: "\(thread.numberOfPages)", attributes: [ + /* thread list view grey page count text */ + .font: pagecountFont, + .foregroundColor: theme[color: "listSecondaryTextColor"]!]) + }(), pageIconColor: theme["threadListPageIconColor"]!, postInfo: { let text: String @@ -159,9 +169,15 @@ extension ThreadListDataSource: UITableViewDataSource { } else { text = String(format: LocalizedString("thread-list.posted-by"), thread.author?.username ?? "") + + var killedByFont: UIFont + if Theme.defaultTheme().roundedFonts { + killedByFont = roundedFont(ofSize: 13, weight: .semibold) + } else { + killedByFont = UIFont.systemFont(ofSize: 13, weight: .medium) } return NSAttributedString(string: text, attributes: [ - .font: UIFont.preferredFontForTextStyle(.footnote, fontName: theme["listFontName"]), + .font: killedByFont, .foregroundColor: theme[color: "listSecondaryTextColor"]!]) }(), ratingImage: { @@ -194,9 +210,19 @@ extension ThreadListDataSource: UITableViewDataSource { return .image(name: thread.threadTag?.imageName, placeholder: placeholder) } }(), - title: NSAttributedString(string: thread.title ?? "", attributes: [ - .font: UIFont.preferredFontForTextStyle(.body, fontName: theme["listFontName"]), - .foregroundColor: theme[color: thread.closed ? "listSecondaryTextColor" : "listTextColor"]!]), + title: { + var threadTitleFont: UIFont + if Theme.defaultTheme().roundedFonts { + threadTitleFont = roundedFont(ofSize: 17, weight: .regular) + } else { + threadTitleFont = UIFont.systemFont(ofSize: 17, weight: .regular) + } + + return NSAttributedString(string: thread.title ?? "", attributes: [ + .font: threadTitleFont, + .foregroundColor: theme[color: thread.closed ? "listSecondaryTextColor" : "listTextColor"]!]) + }() + , unreadCount: { guard thread.beenSeen else { return NSAttributedString() } let color: UIColor @@ -213,9 +239,16 @@ extension ThreadListDataSource: UITableViewDataSource { case .none: color = theme["unreadBadgeBlueColor"]! } } + var unreadCountFont: UIFont + if Theme.defaultTheme().roundedFonts { + unreadCountFont = roundedFont(ofSize: 13, weight: .semibold) + } else { + unreadCountFont = UIFont.systemFont(ofSize: 13, weight: .semibold) + } + return NSAttributedString(string: "\(thread.unreadPosts)", attributes: [ - .font: UIFont.preferredFontForTextStyle(.caption1, fontName: theme["listFontName"], sizeAdjustment: 2), - .foregroundColor: color]) + .font: unreadCountFont, + .foregroundColor: color]) }()) } diff --git a/App/Extensions/UIFont+MonospacedDigits.swift b/App/Extensions/UIFont+MonospacedDigits.swift index 085d67e41..e489c05af 100644 --- a/App/Extensions/UIFont+MonospacedDigits.swift +++ b/App/Extensions/UIFont+MonospacedDigits.swift @@ -19,3 +19,12 @@ extension UIFontDescriptor { return addingAttributes([.featureSettings: featureSettings]) } } + +public func roundedFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont { + // Will be SF Compact or standard SF in case of failure. + if let descriptor = UIFont.systemFont(ofSize: fontSize, weight: weight).fontDescriptor.withDesign(.rounded) { + return UIFont(descriptor: descriptor, size: fontSize) + } else { + return UIFont.systemFont(ofSize: fontSize, weight: weight) + } +} diff --git a/App/Extensions/UIKit.swift b/App/Extensions/UIKit.swift index 76f6aed4d..6c63a971c 100644 --- a/App/Extensions/UIKit.swift +++ b/App/Extensions/UIKit.swift @@ -191,11 +191,19 @@ extension UINavigationItem { label.textAlignment = .center label.textColor = .white label.accessibilityTraits.insert(UIAccessibilityTraits.header) + + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 16, weight: .medium) + } else { + font = UIFont.systemFont(ofSize: 13, weight: .medium) + } + switch UIDevice.current.userInterfaceIdiom { case .pad: - label.font = UIFont.systemFont(ofSize: 17) + label.font = font default: - label.font = UIFont.systemFont(ofSize: 13) + label.font = font label.numberOfLines = 2 } titleView = label diff --git a/App/Navigation/NavigationBar.swift b/App/Navigation/NavigationBar.swift index 569ae9493..073d86601 100644 --- a/App/Navigation/NavigationBar.swift +++ b/App/Navigation/NavigationBar.swift @@ -24,6 +24,12 @@ final class NavigationBar: UINavigationBar { // For whatever reason, translucent navbars with a barTintColor do not necessarily blur their backgrounds. An iPad 3, for example, blurs a bar without a barTintColor but is simply semitransparent with a barTintColor. The semitransparent, non-blur effect looks awful, so just turn it off. isTranslucent = false + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 17, weight: .medium) + } else { + font = UIFont.systemFont(ofSize: 17, weight: .medium) + } // Setting the barStyle to UIBarStyleBlack results in an appropriate status bar style. barStyle = .black @@ -31,8 +37,9 @@ final class NavigationBar: UINavigationBar { backIndicatorImage = UIImage(named: "back") backIndicatorTransitionMaskImage = UIImage(named: "back") - titleTextAttributes = [.font: UIFont.systemFont(ofSize: 17, weight: .regular)] - + titleTextAttributes = [ + .font: font, + ] addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))) if #available(iOS 15.0, *) { diff --git a/App/Navigation/NavigationController.swift b/App/Navigation/NavigationController.swift index c543e9c8f..4b6496713 100644 --- a/App/Navigation/NavigationController.swift +++ b/App/Navigation/NavigationController.swift @@ -97,18 +97,27 @@ final class NavigationController: UINavigationController, Themeable { awfulNavigationBar.layer.shadowOpacity = Float(theme[double: "navigationBarShadowOpacity"] ?? 1) awfulNavigationBar.tintColor = theme["navigationBarTextColor"] - if #available(iOS 15.0, *) { - // Fix odd grey navigation bar background when scrolled to top on iOS 15. - let appearance = UINavigationBarAppearance() - appearance.configureWithOpaqueBackground() - appearance.backgroundColor = theme["navigationBarTintColor"] - - let textColor: UIColor? = theme["navigationBarTextColor"] - appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: textColor!] - - navigationBar.standardAppearance = appearance; - navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 17, weight: .semibold) + } else { + font = UIFont.systemFont(ofSize: 17, weight: .medium) } + + // Fix odd grey navigation bar background when scrolled to top on iOS 15. + let appearance = UINavigationBarAppearance() + appearance.configureWithOpaqueBackground() + appearance.shadowColor = nil + appearance.shadowImage = nil + appearance.backgroundColor = theme["navigationBarTintColor"] + + let textColor: UIColor? = theme["navigationBarTextColor"] + appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: textColor!, + NSAttributedString.Key.font: font + ] + navigationBar.compactAppearance = appearance + navigationBar.standardAppearance = appearance + navigationBar.scrollEdgeAppearance = appearance } override func encodeRestorableState(with coder: NSCoder) { diff --git a/App/Theming/Themes.plist b/App/Theming/Themes.plist old mode 100644 new mode 100755 index de8d72644..5ac4e60e4 --- a/App/Theming/Themes.plist +++ b/App/Theming/Themes.plist @@ -55,6 +55,8 @@ default + roundedFonts + description Light descriptiveColor diff --git a/App/Theming/Themes.swift b/App/Theming/Themes.swift index 07c094885..fd83b86ee 100644 --- a/App/Theming/Themes.swift +++ b/App/Theming/Themes.swift @@ -69,6 +69,10 @@ extension Theme { return dictionary["relevantForumID"] as! String? } + var roundedFonts: Bool { + return dictionary["roundedFonts"] as? Bool ?? false + } + /// The desired appearance for the keyboard. If unspecified by the theme and its ancestors, returns .Default. var keyboardAppearance: UIKeyboardAppearance { let appearance = dictionary["keyboardAppearance"] as? String diff --git a/App/View Controllers/Forums/ForumsTableViewController.swift b/App/View Controllers/Forums/ForumsTableViewController.swift index b493cbcaa..51a3839ad 100644 --- a/App/View Controllers/Forums/ForumsTableViewController.swift +++ b/App/View Controllers/Forums/ForumsTableViewController.swift @@ -101,6 +101,13 @@ final class ForumsTableViewController: TableViewController { private func updateEditingState(favoriteCount: Int) { navigationItem.setRightBarButton(favoriteCount > 0 ? editButtonItem : nil, animated: true) + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 17, weight: .medium) + } else { + font = UIFont.systemFont(ofSize: 17, weight: .medium) + } + editButtonItem.setTitleTextAttributes([NSAttributedString.Key.font: font,], for: .normal) if isEditing, favoriteCount == 0 { setEditing(false, animated: true) } @@ -239,11 +246,18 @@ extension ForumsTableViewController { assertionFailure("where's the header") return nil } + + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 13, weight: .semibold) + } else { + font = UIFont.preferredFont(forTextStyle: .body) + } header.viewModel = .init( backgroundColor: theme["listHeaderBackgroundColor"], - font: UIFont.preferredFont(forTextStyle: .body), sectionName: listDataSource.titleForSection(section), + font: font, textColor: theme["listHeaderTextColor"]) return header diff --git a/App/View Controllers/Messages/MessageListViewController.swift b/App/View Controllers/Messages/MessageListViewController.swift index 382ca3d25..5ff094562 100644 --- a/App/View Controllers/Messages/MessageListViewController.swift +++ b/App/View Controllers/Messages/MessageListViewController.swift @@ -38,6 +38,14 @@ final class MessageListViewController: TableViewController { didChange: updateBadgeValue) updateBadgeValue(unreadMessageCountObserver.count) + var font: UIFont + if Theme.defaultTheme().roundedFonts { + font = roundedFont(ofSize: 17, weight: .medium) + } else { + font = UIFont.systemFont(ofSize: 17, weight: .medium) + } + editButtonItem.setTitleTextAttributes([NSAttributedString.Key.font: font,], for: .normal) + navigationItem.leftBarButtonItem = editButtonItem let composeItem = UIBarButtonItem(image: UIImage(named: "compose"), style: .plain, target: self, action: #selector(MessageListViewController.didTapComposeButtonItem(_:))) composeItem.accessibilityLabel = LocalizedString("private-message-list.compose-button.accessibility-label") diff --git a/App/View Controllers/Posts/PostsPageTopBar.swift b/App/View Controllers/Posts/PostsPageTopBar.swift index c1eb51746..9d7fa6208 100644 --- a/App/View Controllers/Posts/PostsPageTopBar.swift +++ b/App/View Controllers/Posts/PostsPageTopBar.swift @@ -17,7 +17,12 @@ final class PostsPageTopBar: UIView { parentForumButton.accessibilityLabel = LocalizedString("posts-page.parent-forum-button.accessibility-label") parentForumButton.accessibilityHint = LocalizedString("posts-page.parent-forum-button.accessibility-hint") parentForumButton.setTitle(LocalizedString("posts-page.parent-forum-button.title"), for: .normal) - parentForumButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + /* parent forum button */ + if Theme.defaultTheme().roundedFonts { + parentForumButton.titleLabel?.font = roundedFont(ofSize: 12, weight: .medium) + } else { + parentForumButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + } return parentForumButton }() @@ -25,7 +30,12 @@ final class PostsPageTopBar: UIView { let previousPostsButton = UIButton(type: .system) previousPostsButton.accessibilityLabel = LocalizedString("posts-page.previous-posts-button.accessibility-label") previousPostsButton.setTitle(LocalizedString("posts-page.previous-posts-button.title"), for: .normal) - previousPostsButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + /* previous posts button */ + if Theme.defaultTheme().roundedFonts { + previousPostsButton.titleLabel?.font = roundedFont(ofSize: 12, weight: .medium) + } else { + previousPostsButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + } return previousPostsButton }() @@ -33,7 +43,12 @@ final class PostsPageTopBar: UIView { let scrollToEndButton = UIButton(type: .system) scrollToEndButton.accessibilityLabel = LocalizedString("posts-page.scroll-to-end-button.accessibility-label") scrollToEndButton.setTitle(LocalizedString("posts-page.scroll-to-end-button.title"), for: .normal) - scrollToEndButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + /* scroll to end button */ + if Theme.defaultTheme().roundedFonts { + scrollToEndButton.titleLabel?.font = roundedFont(ofSize: 12, weight: .medium) + } else { + scrollToEndButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + } return scrollToEndButton }() diff --git a/App/View Controllers/Threads/ThreadsTableViewController.swift b/App/View Controllers/Threads/ThreadsTableViewController.swift index ef1a82876..c8db8b7b0 100644 --- a/App/View Controllers/Threads/ThreadsTableViewController.swift +++ b/App/View Controllers/Threads/ThreadsTableViewController.swift @@ -251,6 +251,12 @@ final class ThreadsTableViewController: TableViewController, ComposeTextViewCont let title = LocalizedString(filterThreadTag == nil ? "thread-list.filter-button.no-filter" : "thread-list.filter-button.change-filter") filterButton.setTitle(title, for: .normal) + if Theme.defaultTheme().roundedFonts { + filterButton.titleLabel?.font = roundedFont(ofSize: 12, weight: .medium) + } else { + filterButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) + } + filterButton.tintColor = theme["tintColor"] }