@momo-kits/native-kits 0.162.2-beta.1 → 0.162.2-test.1
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.
|
@@ -8,30 +8,23 @@ public func scaleSize(_ size: CGFloat, _ scaleRate: CGFloat? = nil) -> CGFloat {
|
|
|
8
8
|
let deviceWidth = UIScreen.main.bounds.width
|
|
9
9
|
let deviceScale = deviceWidth / defaultScreenSize
|
|
10
10
|
|
|
11
|
+
let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
|
|
12
|
+
let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
|
|
13
|
+
let fontScale = scaledFont.pointSize / defaultFont.pointSize
|
|
14
|
+
|
|
11
15
|
var fontSizeDeviceScale = size
|
|
12
|
-
var
|
|
16
|
+
var fontSizeOSScale = size
|
|
17
|
+
|
|
13
18
|
|
|
14
19
|
if deviceScale > 1 {
|
|
15
20
|
fontSizeDeviceScale = min(fontSizeDeviceScale * deviceScale, fontSizeDeviceScale + maxDeviceScale)
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
if
|
|
19
|
-
|
|
20
|
-
let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
|
|
21
|
-
let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
|
|
22
|
-
let fontScale = scaledFont.pointSize / defaultFont.pointSize
|
|
23
|
-
if fontScale > 1 {
|
|
24
|
-
fontSizeUserScale = min(fontSizeUserScale * fontScale, fontSizeUserScale * maxFontScale)
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
// Custom in-app ratio. OS font scale is ignored.
|
|
28
|
-
let ratio = MomoFontScale.shared.ratio
|
|
29
|
-
if ratio > 1 {
|
|
30
|
-
fontSizeUserScale = fontSizeUserScale * ratio
|
|
31
|
-
}
|
|
23
|
+
if fontScale > 1 {
|
|
24
|
+
fontSizeOSScale = min(fontSizeOSScale * fontScale, fontSizeOSScale * maxFontScale)
|
|
32
25
|
}
|
|
33
26
|
|
|
34
|
-
return max(fontSizeDeviceScale,
|
|
27
|
+
return max(fontSizeDeviceScale, fontSizeOSScale)
|
|
35
28
|
}
|
|
36
29
|
|
|
37
30
|
public enum TypographyStyle {
|
|
@@ -125,9 +118,6 @@ public struct MomoText: View {
|
|
|
125
118
|
private let content: String
|
|
126
119
|
private let typography: TypographyStyle
|
|
127
120
|
private let color: Color
|
|
128
|
-
// Observe so the text re-renders immediately when the user changes the
|
|
129
|
-
// font-size setting (live update, no app restart).
|
|
130
|
-
@ObservedObject private var fontScale = MomoFontScale.shared
|
|
131
121
|
|
|
132
122
|
public init(
|
|
133
123
|
_ content: String,
|
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
import SwiftUI
|
|
2
2
|
|
|
3
3
|
public extension Font {
|
|
4
|
-
// Shares the same scaling logic as `scaleSize` so it honors the user's
|
|
5
|
-
// font-size preference (follow-OS vs in-app ratio).
|
|
6
|
-
// NOTE: the `static let` constants below are evaluated once on first
|
|
7
|
-
// access, so they reflect the setting at launch but do not live-update.
|
|
8
|
-
// Components that need live updates should use `MomoText` / `scaleSize`.
|
|
9
4
|
static func appFont(size: CGFloat) -> Font {
|
|
10
|
-
|
|
5
|
+
let defaultScreenSize: CGFloat = 375
|
|
6
|
+
let maxFontScale: CGFloat = 1.5
|
|
7
|
+
let maxDeviceScale: CGFloat = 5
|
|
8
|
+
|
|
9
|
+
let deviceWidth = UIScreen.main.bounds.width
|
|
10
|
+
let deviceScale = deviceWidth / defaultScreenSize
|
|
11
|
+
|
|
12
|
+
let defaultFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
|
|
13
|
+
let scaledFont = UIFontMetrics.default.scaledFont(for: defaultFont)
|
|
14
|
+
let fontScale = scaledFont.pointSize / defaultFont.pointSize
|
|
15
|
+
|
|
16
|
+
var fontSize = size
|
|
17
|
+
|
|
18
|
+
if deviceScale > 1 {
|
|
19
|
+
fontSize = min(fontSize * deviceScale, fontSize + maxDeviceScale)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if fontScale > 1 {
|
|
23
|
+
fontSize = min(fontSize * fontScale, fontSize * maxFontScale)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return Font.system(size: fontSize)
|
|
11
27
|
}
|
|
12
28
|
|
|
13
29
|
// New supported typography styles
|
package/package.json
CHANGED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import SwiftUI
|
|
2
|
-
|
|
3
|
-
/// Process-wide holder for the user's in-app font-size preference.
|
|
4
|
-
///
|
|
5
|
-
/// `momo-native-kits` is a UI library; it does not read persistent storage
|
|
6
|
-
/// itself. The host app (momo-app) reads the persisted setting from the shared
|
|
7
|
-
/// Observable Storage and pushes it here at launch and on every change via
|
|
8
|
-
/// `update(followOSScale:ratio:)`. All native typography (`scaleSize`,
|
|
9
|
-
/// `Font.appFont`, `MomoText`) reads from this single source so the three
|
|
10
|
-
/// render stacks (RN / Compose / SwiftUI) stay in sync.
|
|
11
|
-
///
|
|
12
|
-
/// Live update: `MomoText` observes this object, so changing the setting
|
|
13
|
-
/// re-renders typed text immediately. Views built from the `Font.appFont`
|
|
14
|
-
/// static constants pick up the value at launch (static `let` is evaluated
|
|
15
|
-
/// once); making those live would require turning them into functions.
|
|
16
|
-
public final class MomoFontScale: ObservableObject {
|
|
17
|
-
public static let shared = MomoFontScale()
|
|
18
|
-
|
|
19
|
-
/// When true, follow the OS font scale (capped). `ratio` is ignored.
|
|
20
|
-
@Published public private(set) var followOSScale: Bool = false
|
|
21
|
-
|
|
22
|
-
/// In-app font-size ratio (e.g. 1.0 / 1.1 / 1.2). Ignored when
|
|
23
|
-
/// `followOSScale` is true.
|
|
24
|
-
@Published public private(set) var ratio: CGFloat = 1
|
|
25
|
-
|
|
26
|
-
private init() {}
|
|
27
|
-
|
|
28
|
-
public func update(followOSScale: Bool, ratio: CGFloat) {
|
|
29
|
-
if self.followOSScale != followOSScale {
|
|
30
|
-
self.followOSScale = followOSScale
|
|
31
|
-
}
|
|
32
|
-
if self.ratio != ratio {
|
|
33
|
-
self.ratio = ratio
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|