Skip to content

Commit

Permalink
APP字体大小跟随系统设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Oct 25, 2024
1 parent efae838 commit 8a0b40c
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 35 deletions.
8 changes: 8 additions & 0 deletions Bark.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
067B2EB525693E38008B6BE1 /* MessageTableViewCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 067B2EB425693E38008B6BE1 /* MessageTableViewCellViewModel.swift */; };
06802E5320ECC40C00767047 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0661A549204FDA4100965E4E /* Assets.xcassets */; };
06840DBB272298FB001B3193 /* BKColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06840DBA272298FB001B3193 /* BKColor.swift */; };
0687F2A82CCB791A00B2A52F /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0687F2A72CCB791A00B2A52F /* UIFont+Extension.swift */; };
0687F2A92CCB791A00B2A52F /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0687F2A72CCB791A00B2A52F /* UIFont+Extension.swift */; };
0687F2AA2CCB7FA500B2A52F /* UIFont+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0687F2A72CCB791A00B2A52F /* UIFont+Extension.swift */; };
06885EB6247FB9880004A303 /* MessageSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06885EB5247FB9880004A303 /* MessageSettingsViewController.swift */; };
0689CF4C2C7484A7007203A6 /* BarkTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0689CF4B2C7484A7007203A6 /* BarkTabBarController.swift */; };
068EC15827ED99C900D5D11E /* ServerListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 068EC15727ED99C900D5D11E /* ServerListViewController.swift */; };
Expand Down Expand Up @@ -326,6 +329,7 @@
0683487020510FB20024B6DA /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = System/Library/Frameworks/UserNotifications.framework; sourceTree = SDKROOT; };
0683487220510FB20024B6DA /* UserNotificationsUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotificationsUI.framework; path = System/Library/Frameworks/UserNotificationsUI.framework; sourceTree = SDKROOT; };
06840DBA272298FB001B3193 /* BKColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BKColor.swift; sourceTree = "<group>"; };
0687F2A72CCB791A00B2A52F /* UIFont+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Extension.swift"; sourceTree = "<group>"; };
06885EB5247FB9880004A303 /* MessageSettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSettingsViewController.swift; sourceTree = "<group>"; };
0689CF4B2C7484A7007203A6 /* BarkTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarkTabBarController.swift; sourceTree = "<group>"; };
068EC15727ED99C900D5D11E /* ServerListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerListViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -582,6 +586,7 @@
06F08EA629B1DDFE006AB9CA /* Error+Extension.swift */,
06F08EAB29B1DECD006AB9CA /* NSLocalizedString+Extension.swift */,
06E944742C07012E00AC86AB /* RealmConfiguration.swift */,
0687F2A72CCB791A00B2A52F /* UIFont+Extension.swift */,
);
path = Common;
sourceTree = "<group>";
Expand Down Expand Up @@ -1103,6 +1108,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0687F2AA2CCB7FA500B2A52F /* UIFont+Extension.swift in Sources */,
0632CE2320EC9098003FDF46 /* NotificationViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1138,6 +1144,7 @@
066E0C8C2BB6AC9A00873838 /* AddSoundCell.swift in Sources */,
065BE4502563D939002A8CA4 /* SoundCellViewModel.swift in Sources */,
06F08EA729B1DDFE006AB9CA /* Error+Extension.swift in Sources */,
0687F2A82CCB791A00B2A52F /* UIFont+Extension.swift in Sources */,
06B1158F247BB1FB006D91FB /* Message.swift in Sources */,
06172FDA27F6DAEF002333A4 /* ServerListTableViewCell.swift in Sources */,
0653677829B727A60038BDB8 /* CryptoSettingRelay.swift in Sources */,
Expand Down Expand Up @@ -1218,6 +1225,7 @@
06E9447C2C07052F00AC86AB /* ImageProcessor.swift in Sources */,
06E9446A2C06E4A200AC86AB /* CiphertextProcessor.swift in Sources */,
06E944712C06FF4C00AC86AB /* AutoCopyProcessor.swift in Sources */,
0687F2A92CCB791A00B2A52F /* UIFont+Extension.swift in Sources */,
06F08EA829B1DE0A006AB9CA /* Error+Extension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
15 changes: 15 additions & 0 deletions Common/UIFont+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// UIFont+Extension.swift
// Bark
//
// Created by huangfeng on 10/25/24.
// Copyright © 2024 Fin. All rights reserved.
//

import UIKit

extension UIFont {
class func preferredFont(ofSize size: CGFloat, weight: Weight = .regular) -> UIFont {
return UIFontMetrics.default.scaledFont(for: UIFont.systemFont(ofSize: size, weight: weight))
}
}
9 changes: 6 additions & 3 deletions Controller/CrashReportViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class CrashReportViewController: UIViewController {

let crashedTitle = UILabel()
crashedTitle.text = NSLocalizedString("crashed")
crashedTitle.font = UIFont.boldSystemFont(ofSize: 30)
crashedTitle.font = UIFont.preferredFont(ofSize: 30, weight: .bold)
crashedTitle.adjustsFontForContentSizeCategory = true
crashedTitle.textColor = UIColor(r255: 239, g255: 77, b255: 77)
self.view.addSubview(crashedTitle)

Expand All @@ -31,13 +32,15 @@ class CrashReportViewController: UIViewController {
contentlabel.isScrollEnabled = false
contentlabel.textContainerInset = .zero
contentlabel.textContainer.lineFragmentPadding = 0
contentlabel.font = UIFont.systemFont(ofSize: 14)
contentlabel.font = UIFont.preferredFont(ofSize: 14)
contentlabel.adjustsFontForContentSizeCategory = true
contentlabel.textColor = UIColor(r255: 51, g255: 51, b255: 51)
contentlabel.text = NSLocalizedString("crashContent")
self.view.addSubview(contentlabel)

let copyButton = UIButton()
copyButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
copyButton.titleLabel?.font = UIFont.preferredFont(ofSize: 16, weight: .bold)
copyButton.titleLabel?.adjustsFontForContentSizeCategory = true
copyButton.setTitleColor(UIColor.white, for: .normal)
copyButton.setTitle(NSLocalizedString("copyCrashLog"), for: .normal)
copyButton.backgroundColor = UIColor(r255: 239, g255: 77, b255: 77)
Expand Down
12 changes: 8 additions & 4 deletions Controller/CryptoSettingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {

let keyTextField: BorderTextField = {
let textField = BorderTextField(title: "Key")
textField.font = UIFont.systemFont(ofSize: 14)
textField.font = UIFont.preferredFont(ofSize: 14)
textField.adjustsFontForContentSizeCategory = true
textField.placeholder = String(format: NSLocalizedString("enterKey"), 16)
return textField
}()

let ivTextField: BorderTextField = {
let textField = BorderTextField(title: "IV")
textField.font = UIFont.systemFont(ofSize: 14)
textField.font = UIFont.preferredFont(ofSize: 14)
textField.adjustsFontForContentSizeCategory = true
textField.placeholder = String(format: NSLocalizedString("enterIv"), 16) // todo: update iv length
return textField
}()
Expand All @@ -41,7 +43,8 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
let btn = GradientButton()
btn.setTitle(NSLocalizedString("copyExample"), for: .normal)
btn.setTitleColor(UIColor.white, for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
btn.titleLabel?.font = UIFont.preferredFont(ofSize: 14, weight: .medium)
btn.titleLabel?.adjustsFontForContentSizeCategory = true
btn.layer.cornerRadius = 8
btn.clipsToBounds = true
btn.applyGradient(
Expand Down Expand Up @@ -70,7 +73,8 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {

func getTitleLabel(title: String) -> UILabel {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken4
label.text = title
return label
Expand Down
3 changes: 2 additions & 1 deletion Controller/NewServerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class NewServerViewController: BaseViewController<NewServerViewModel> {
let label = UILabel()
label.text = NSLocalizedString("DeploymentDocuments")
label.textColor = BKColor.blue.base
label.font = UIFont.systemFont(ofSize: 12)
label.font = UIFont.preferredFont(ofSize: 12)
label.adjustsFontForContentSizeCategory = true
label.transition([.scale(0.85), .opacity(0), .translate(x: 50)])
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer())
Expand Down
4 changes: 2 additions & 2 deletions Controller/SoundsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ extension SoundsViewController: UITableViewDelegate {
string: fullText,
attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken3,
NSAttributedString.Key.font: RobotoFont.regular(with: 14)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: 14)
]
)
attrStr.setAttributes([
NSAttributedString.Key.foregroundColor: BKColor.lightBlue.darken3,
NSAttributedString.Key.font: RobotoFont.regular(with: 14)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: 14)
], range: (fullText as NSString).range(of: highlightText))

let label = UILabel()
Expand Down
3 changes: 2 additions & 1 deletion View/AddSoundCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class AddSoundCell: UITableViewCell {
button.setImage(UIImage(named: "music_note-music_note_symbol"), for: .normal)
button.setTitleColor(BKColor.lightBlue.darken3, for: .normal)
button.tintColor = BKColor.lightBlue.darken3
button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
button.titleLabel?.font = UIFont.preferredFont(ofSize: 16)
button.titleLabel?.adjustsFontForContentSizeCategory = true
// 从 UITableView didSelectRowAt 那响应点击事件
button.isUserInteractionEnabled = false
return button
Expand Down
5 changes: 3 additions & 2 deletions View/BorderTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class BorderTextField: InsetTextField {
self.insets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)

self.textColor = BKColor.grey.darken3
self.font = UIFont.systemFont(ofSize: 14)
self.font = UIFont.preferredFont(ofSize: 14)
self.adjustsFontForContentSizeCategory = true
self.textAlignment = .left

self.insertSubview(backgroundView, at: 0)
Expand All @@ -78,7 +79,7 @@ class BorderTextField: InsetTextField {
override var placeholder: String? {
didSet{
self.attributedPlaceholder = NSAttributedString(string: placeholder ?? "" , attributes: [
.font: self.font ?? UIFont.systemFont(ofSize: 14),
.font: self.font ?? UIFont.preferredFont(ofSize: 14),
.foregroundColor: BKColor.grey.darken1
])
}
Expand Down
3 changes: 2 additions & 1 deletion View/DropBoxView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class DropBoxView: UIView {

let valueLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken3
return label
}()
Expand Down
16 changes: 9 additions & 7 deletions View/MessageTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
label.isScrollEnabled = false
label.textContainerInset = .zero
label.textContainer.lineFragmentPadding = 0
label.font = RobotoFont.regular(with: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken4
return label
}()

let dateLabel: UILabel = {
let label = BKLabel()
label.hitTestSlop = UIEdgeInsets(top: -5, left: -5, bottom: -5, right: -5)
label.font = RobotoFont.medium(with: 11)
label.font = UIFont.preferredFont(ofSize: 11, weight: .medium)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.base
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer())
Expand Down Expand Up @@ -97,31 +99,31 @@ class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {

let text = NSMutableAttributedString(
string: body,
attributes: [.font: RobotoFont.regular(with: 14), .foregroundColor: BKColor.grey.darken4]
attributes: [.font: UIFont.preferredFont(ofSize: 14), .foregroundColor: BKColor.grey.darken4]
)

if title.count > 0 {
// 插入一行空行当 spacer
text.insert(NSAttributedString(
string: "\n",
attributes: [.font: RobotoFont.medium(with: 6)]
attributes: [.font: UIFont.systemFont(ofSize: 6, weight: .medium)]
), at: 0)

text.insert(NSAttributedString(
string: title + "\n",
attributes: [.font: RobotoFont.medium(with: 16), .foregroundColor: BKColor.grey.darken4]
attributes: [.font: UIFont.preferredFont(ofSize: 16, weight: .medium), .foregroundColor: BKColor.grey.darken4]
), at: 0)
}

if url.count > 0 {
// 插入一行空行当 spacer
text.append(NSAttributedString(
string: "\n ",
attributes: [.font: RobotoFont.medium(with: 8)]
attributes: [.font: UIFont.systemFont(ofSize: 8, weight: .medium)]
))

text.append(NSAttributedString(string: "\n\(url)", attributes: [
.font: RobotoFont.regular(with: 14),
.font: UIFont.preferredFont(ofSize: 14),
.foregroundColor: BKColor.grey.darken4,
.link: url
]))
Expand Down
12 changes: 8 additions & 4 deletions View/PreviewCardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ class PreviewCardCell: BaseTableViewCell<PreviewCardCellViewModel> {

let titleLabel: UILabel = {
let label = UILabel()
label.font = RobotoFont.regular(with: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken3
label.numberOfLines = 0
return label
}()

let bodyLabel: UILabel = {
let label = UILabel()
label.font = RobotoFont.regular(with: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken2
label.numberOfLines = 0
return label
}()

let noticeLabel: UILabel = {
let label = UILabel()
label.font = RobotoFont.regular(with: 12)
label.font = UIFont.preferredFont(ofSize: 12)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.base
label.numberOfLines = 0
label.isUserInteractionEnabled = true
Expand All @@ -58,7 +61,8 @@ class PreviewCardCell: BaseTableViewCell<PreviewCardCellViewModel> {
let label = UILabel()
label.lineBreakMode = .byCharWrapping
label.numberOfLines = 0
label.font = RobotoFont.regular(with: 14)
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
return label
}()

Expand Down
14 changes: 7 additions & 7 deletions View/PreviewCardCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class PreviewCardCellViewModel: ViewModel {
let noticeStr = "\(previewModel.notice ?? "")"
let noticeAttrStr = NSMutableAttributedString(string: noticeStr, attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.base,
NSAttributedString.Key.font: RobotoFont.regular(with: 12)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: 12)
])

if let moreInfo = previewModel.moreInfo {
noticeAttrStr.append(NSMutableAttributedString(string: " \(moreInfo)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.blue.base,
NSAttributedString.Key.font: RobotoFont.regular(with: 12)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: 12)
]))
}
notice.accept(noticeAttrStr)
Expand All @@ -69,30 +69,30 @@ class PreviewCardCellViewModel: ViewModel {
let attrStr = NSMutableAttributedString(string: "")
attrStr.append(NSAttributedString(string: serverUrl.absoluteString, attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken4,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: fontSize)
]))
let key = ServerManager.shared.currentServer.key
attrStr.append(NSAttributedString(string: "/\(key.count > 0 ? key : "Your Key")", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken3,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: fontSize)
]))

if let modelTitle = previewModel.title {
attrStr.append(NSAttributedString(string: "/\(modelTitle)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken1,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: fontSize)
]))
}
if let modelBody = previewModel.body {
attrStr.append(NSAttributedString(string: "/\(modelBody)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.base,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: fontSize)
]))
}
if let queryParameter = previewModel.queryParameter {
attrStr.append(NSAttributedString(string: "?\(queryParameter)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.lighten1,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
NSAttributedString.Key.font: UIFont.preferredFont(ofSize: fontSize)
]))
}

Expand Down
6 changes: 4 additions & 2 deletions View/ServerListTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ class ServerListTableViewCell: BaseTableViewCell<ServerListTableViewCellViewMode

let addressLabel: UILabel = {
let label = UILabel()
label.font = RobotoFont.medium(with: 14)
label.font = UIFont.preferredFont(ofSize: 14, weight: .medium)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken4
label.numberOfLines = 0
return label
}()

let keyLabel: UILabel = {
let label = UILabel()
label.font = RobotoFont.regular(with: 12)
label.font = UIFont.preferredFont(ofSize: 12)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken4
label.numberOfLines = 0
return label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class NotificationViewController: UIViewController, UNNotificationContentExtensi
let label = UILabel()
label.textColor = UIColor(named: "notification_copy_color")
label.text = NSLocalizedString("Copy", comment: "")
label.font = UIFont.systemFont(ofSize: 16)
label.font = UIFont.preferredFont(ofSize: 16)
label.adjustsFontForContentSizeCategory = true
label.textAlignment = .center
return label
}()
Expand Down

0 comments on commit 8a0b40c

Please sign in to comment.