Skip to content

Commit

Permalink
Support rounded fonts in themes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfsm authored and nolanw committed Jun 12, 2022
1 parent e196f62 commit ca8098a
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 32 deletions.
20 changes: 16 additions & 4 deletions App/Data Sources/MessageListDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
51 changes: 42 additions & 9 deletions App/Data Sources/ThreadListDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand Down Expand Up @@ -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
Expand All @@ -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])
}())
}

Expand Down
9 changes: 9 additions & 0 deletions App/Extensions/UIFont+MonospacedDigits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 10 additions & 2 deletions App/Extensions/UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions App/Navigation/NavigationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ 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

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, *) {
Expand Down
31 changes: 20 additions & 11 deletions App/Navigation/NavigationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions App/Theming/Themes.plist
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
</dict>
<key>default</key>
<dict>
<key>roundedFonts</key>
<false/>
<key>description</key>
<string>Light</string>
<key>descriptiveColor</key>
Expand Down
4 changes: 4 additions & 0 deletions App/Theming/Themes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion App/View Controllers/Forums/ForumsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions App/View Controllers/Messages/MessageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 18 additions & 3 deletions App/View Controllers/Posts/PostsPageTopBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,38 @@ 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
}()

private lazy var previousPostsButton: UIButton = {
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
}()

private let scrollToEndButton: UIButton = {
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
}()

Expand Down
6 changes: 6 additions & 0 deletions App/View Controllers/Threads/ThreadsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

Expand Down

0 comments on commit ca8098a

Please sign in to comment.