@momo-kits/native-kits 0.162.3-fontscale.1 → 0.162.3-test.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import SwiftUI
|
|
2
|
+
|
|
3
|
+
public struct FontScaleConfig {
|
|
4
|
+
public var useOSFontScale: Bool
|
|
5
|
+
public var userScaleRate: CGFloat?
|
|
6
|
+
|
|
7
|
+
public init(useOSFontScale: Bool = true, userScaleRate: CGFloat? = nil) {
|
|
8
|
+
self.useOSFontScale = useOSFontScale
|
|
9
|
+
self.userScaleRate = userScaleRate
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public final class FontScaleStore: ObservableObject {
|
|
14
|
+
public static let shared = FontScaleStore()
|
|
15
|
+
|
|
16
|
+
@Published public private(set) var config = FontScaleConfig()
|
|
17
|
+
|
|
18
|
+
private var didBind = false
|
|
19
|
+
private var observerId: String?
|
|
20
|
+
|
|
21
|
+
private init() {}
|
|
22
|
+
|
|
23
|
+
public func update(_ newConfig: FontScaleConfig) {
|
|
24
|
+
if Thread.isMainThread {
|
|
25
|
+
config = newConfig
|
|
26
|
+
} else {
|
|
27
|
+
DispatchQueue.main.async { [weak self] in self?.config = newConfig }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public func bind(_ api: KitComposeApi?) {
|
|
32
|
+
guard let api = api, !didBind else { return }
|
|
33
|
+
didBind = true
|
|
34
|
+
|
|
35
|
+
apply(api.request(funcName: "getFontScaleConfig", params: nil))
|
|
36
|
+
observerId = api.request(funcName: "observer", params: ["font_scale_config"]) { [weak self] response in
|
|
37
|
+
self?.apply(response)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private func apply(_ response: String) {
|
|
42
|
+
guard let parsed = FontScaleStore.parse(response) else { return }
|
|
43
|
+
update(parsed)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static func parse(_ response: String) -> FontScaleConfig? {
|
|
47
|
+
guard
|
|
48
|
+
let data = response.data(using: .utf8),
|
|
49
|
+
let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
|
|
50
|
+
else { return nil }
|
|
51
|
+
|
|
52
|
+
let obj = (json["response"] as? [String: Any]) ?? (json["data"] as? [String: Any]) ?? json
|
|
53
|
+
guard obj["useOSFontScale"] != nil || obj["userScaleRate"] != nil else { return nil }
|
|
54
|
+
|
|
55
|
+
let useOS = (obj["useOSFontScale"] as? Bool) ?? true
|
|
56
|
+
let rate = (obj["userScaleRate"] as? NSNumber).map { CGFloat($0.doubleValue) }
|
|
57
|
+
return FontScaleConfig(useOSFontScale: useOS, userScaleRate: rate)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
import SwiftUI
|
|
2
2
|
|
|
3
3
|
public func scaleSize(_ size: CGFloat, _ scaleRate: CGFloat? = nil) -> CGFloat {
|
|
4
|
+
let config = FontScaleStore.shared.config
|
|
4
5
|
let defaultScreenSize: CGFloat = 375
|
|
5
6
|
let maxFontScale: CGFloat = scaleRate ?? 1.5
|
|
6
7
|
let maxDeviceScale: CGFloat = 5
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
if !config.useOSFontScale {
|
|
10
|
+
let customRate = config.userScaleRate ?? 1
|
|
11
|
+
return customRate > 1 ? size * customRate : size
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let deviceScale = UIScreen.main.bounds.width / defaultScreenSize
|
|
10
15
|
|
|
11
16
|
let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
|
|
12
17
|
let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
|
|
13
|
-
let
|
|
18
|
+
let osFontScale = scaledFont.pointSize / defaultFont.pointSize
|
|
14
19
|
|
|
15
20
|
var fontSizeDeviceScale = size
|
|
16
21
|
var fontSizeOSScale = size
|
|
17
22
|
|
|
18
|
-
|
|
19
23
|
if deviceScale > 1 {
|
|
20
24
|
fontSizeDeviceScale = min(fontSizeDeviceScale * deviceScale, fontSizeDeviceScale + maxDeviceScale)
|
|
21
25
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
fontSizeOSScale = min(fontSizeOSScale * fontScale, fontSizeOSScale * maxFontScale)
|
|
26
|
+
if osFontScale > 1 {
|
|
27
|
+
fontSizeOSScale = min(fontSizeOSScale * osFontScale, fontSizeOSScale * maxFontScale)
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
return max(fontSizeDeviceScale, fontSizeOSScale)
|
|
@@ -118,6 +121,7 @@ public struct MomoText: View {
|
|
|
118
121
|
private let content: String
|
|
119
122
|
private let typography: TypographyStyle
|
|
120
123
|
private let color: Color
|
|
124
|
+
@ObservedObject private var fontScale = FontScaleStore.shared
|
|
121
125
|
|
|
122
126
|
public init(
|
|
123
127
|
_ content: String,
|
|
@@ -130,6 +134,7 @@ public struct MomoText: View {
|
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
public var body: some View {
|
|
137
|
+
_ = fontScale.config
|
|
133
138
|
let text = SwiftUI.Text(content)
|
|
134
139
|
.font(.system(size: scaleSize(typography.fontSize), weight: typography.fontWeight))
|
|
135
140
|
.foregroundColor(color)
|
|
@@ -11,30 +11,35 @@ public extension View {
|
|
|
11
11
|
|
|
12
12
|
public extension Font {
|
|
13
13
|
static func appFont(size: CGFloat) -> Font {
|
|
14
|
+
let config = FontScaleStore.shared.config
|
|
14
15
|
let defaultScreenSize: CGFloat = 375
|
|
15
16
|
let maxFontScale: CGFloat = 1.5
|
|
16
17
|
let maxDeviceScale: CGFloat = 5
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
if !config.useOSFontScale {
|
|
20
|
+
let customRate = config.userScaleRate ?? 1
|
|
21
|
+
let scaled = customRate > 1 ? size * customRate : size
|
|
22
|
+
return Font.system(size: scaled)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let deviceScale = UIScreen.main.bounds.width / defaultScreenSize
|
|
20
26
|
|
|
21
27
|
let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
|
|
22
28
|
let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
|
|
23
|
-
let
|
|
29
|
+
let osFontScale = scaledFont.pointSize / defaultFont.pointSize
|
|
24
30
|
|
|
25
31
|
var fontSize = size
|
|
26
32
|
|
|
27
33
|
if deviceScale > 1 {
|
|
28
34
|
fontSize = min(fontSize * deviceScale, fontSize + maxDeviceScale)
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
fontSize = min(fontSize * fontScale, fontSize * maxFontScale)
|
|
36
|
+
if osFontScale > 1 {
|
|
37
|
+
fontSize = min(fontSize * osFontScale, fontSize * maxFontScale)
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
return Font.system(size: fontSize)
|
|
36
41
|
}
|
|
37
|
-
|
|
42
|
+
|
|
38
43
|
// New supported typography styles
|
|
39
44
|
static let headline_default_bold = appFont(size: 24).weight(.bold)
|
|
40
45
|
static let header_m_bold = appFont(size: 18).weight(.bold)
|
|
@@ -52,7 +57,7 @@ public extension Font {
|
|
|
52
57
|
static let action_s_bold = appFont(size: 14).weight(.bold)
|
|
53
58
|
static let action_xs_bold = appFont(size: 12).weight(.bold)
|
|
54
59
|
static let action_xxs_bold = appFont(size: 10).weight(.bold)
|
|
55
|
-
|
|
60
|
+
|
|
56
61
|
// Legacy styles
|
|
57
62
|
static let headline_default = appFont(size: 28).weight(.semibold)
|
|
58
63
|
static let headline_s = appFont(size: 24).weight(.semibold)
|
|
@@ -77,7 +82,7 @@ public extension Font {
|
|
|
77
82
|
static let action_xxs = appFont(size: 10).weight(.bold)
|
|
78
83
|
static let action_xs = appFont(size: 12).weight(.bold)
|
|
79
84
|
static let action_s = appFont(size: 15).weight(.bold)
|
|
80
|
-
|
|
85
|
+
|
|
81
86
|
// deprecated
|
|
82
87
|
static let h1 = appFont(size: 36).weight(.semibold) //headline_xl
|
|
83
88
|
static let h2 = appFont(size: 32).weight(.semibold)
|