Skip to content

Commit 51c281d

Browse files
authored
fix: adjust default font size per platform (#379)
1 parent 993b1aa commit 51c281d

File tree

4 files changed

+103
-46
lines changed

4 files changed

+103
-46
lines changed

.changeset/fancy-papayas-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-native-bottom-tabs': patch
3+
---
4+
5+
fix: adjust default font size on iOS

packages/react-native-bottom-tabs/ios/PlatformAliases.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ typealias PlatformViewRepresentable = NSViewRepresentable
3535
#else
3636
typealias PlatformViewRepresentable = UIViewRepresentable
3737
#endif
38+
39+
#if os(macOS)
40+
typealias PlatformFont = NSFont
41+
#else
42+
typealias PlatformFont = UIFont
43+
#endif
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import UIKit
2+
import React
3+
4+
enum TabBarFontSize {
5+
/// Returns the default font size for tab bar item labels based on the current platform
6+
#if os(tvOS)
7+
static let defaultSize: CGFloat = 30.0
8+
#else
9+
static let defaultSize: CGFloat = {
10+
if UIDevice.current.userInterfaceIdiom == .pad {
11+
return 13.0
12+
}
13+
14+
return 10.0
15+
}()
16+
#endif
17+
18+
/// Creates font attributes for tab bar items
19+
/// - Parameters:
20+
/// - size: Font size in points
21+
/// - family: Optional font family name
22+
/// - weight: Optional font weight string
23+
/// - color: Optional text color
24+
/// - Returns: Dictionary of NSAttributedString attributes
25+
static func createFontAttributes(
26+
size: CGFloat,
27+
family: String? = nil,
28+
weight: String? = nil,
29+
color: PlatformColor? = nil
30+
) -> [NSAttributedString.Key: Any] {
31+
var attributes: [NSAttributedString.Key: Any] = [:]
32+
33+
// Create font with React Native font handling if family or weight is specified
34+
if family != nil || weight != nil {
35+
attributes[.font] = RCTFont.update(
36+
nil,
37+
withFamily: family,
38+
size: NSNumber(value: size),
39+
weight: weight,
40+
style: nil,
41+
variant: nil,
42+
scaleMultiplier: 1.0
43+
)
44+
} else {
45+
// Fallback to system font
46+
#if os(macOS)
47+
attributes[.font] = NSFont.boldSystemFont(ofSize: size)
48+
#else
49+
attributes[.font] = UIFont.boldSystemFont(ofSize: size)
50+
#endif
51+
}
52+
53+
// Add color if provided
54+
if let color = color {
55+
attributes[.foregroundColor] = color
56+
}
57+
58+
return attributes
59+
}
60+
61+
/// Creates font attributes specifically for normal (unselected) tab state
62+
/// - Parameters:
63+
/// - fontSize: Optional font size (uses default if nil)
64+
/// - fontFamily: Optional font family
65+
/// - fontWeight: Optional font weight
66+
/// - inactiveColor: Optional color for inactive state
67+
/// - Returns: Font attributes dictionary
68+
static func createNormalStateAttributes(
69+
fontSize: Int? = nil,
70+
fontFamily: String? = nil,
71+
fontWeight: String? = nil,
72+
inactiveColor: PlatformColor? = nil
73+
) -> [NSAttributedString.Key: Any] {
74+
let size = fontSize.map(CGFloat.init) ?? defaultSize
75+
return createFontAttributes(
76+
size: size,
77+
family: fontFamily,
78+
weight: fontWeight,
79+
color: inactiveColor
80+
)
81+
}
82+
}

packages/react-native-bottom-tabs/ios/TabViewImpl.swift

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -117,36 +117,6 @@ struct TabViewImpl: View {
117117
}
118118
#endif
119119

120-
private func createFontAttributes(
121-
size: CGFloat,
122-
family: String?,
123-
weight: String?,
124-
inactiveTintColor: PlatformColor?
125-
) -> [NSAttributedString.Key: Any] {
126-
var attributes: [NSAttributedString.Key: Any] = [:]
127-
128-
if family != nil || weight != nil {
129-
attributes[.font] = RCTFont.update(
130-
nil,
131-
withFamily: family,
132-
size: NSNumber(value: size),
133-
weight: weight,
134-
style: nil,
135-
variant: nil,
136-
scaleMultiplier: 1.0
137-
)
138-
} else {
139-
attributes[.font] = UIFont.boldSystemFont(ofSize: size)
140-
}
141-
142-
return attributes
143-
}
144-
145-
#if os(tvOS)
146-
let tabBarDefaultFontSize: CGFloat = 30.0
147-
#else
148-
let tabBarDefaultFontSize: CGFloat = UIFont.smallSystemFontSize
149-
#endif
150120

151121
#if !os(macOS)
152122
private func configureTransparentAppearance(tabBar: UITabBar, props: TabViewProps) {
@@ -158,12 +128,11 @@ private func createFontAttributes(
158128

159129
guard let items = tabBar.items else { return }
160130

161-
let fontSize = props.fontSize != nil ? CGFloat(props.fontSize!) : tabBarDefaultFontSize
162-
let attributes = createFontAttributes(
163-
size: fontSize,
164-
family: props.fontFamily,
165-
weight: props.fontWeight,
166-
inactiveTintColor: nil
131+
let attributes = TabBarFontSize.createNormalStateAttributes(
132+
fontSize: props.fontSize,
133+
fontFamily: props.fontFamily,
134+
fontWeight: props.fontWeight,
135+
inactiveColor: nil
167136
)
168137

169138
items.forEach { item in
@@ -192,19 +161,14 @@ private func createFontAttributes(
192161

193162
// Configure item appearance
194163
let itemAppearance = UITabBarItemAppearance()
195-
let fontSize = props.fontSize != nil ? CGFloat(props.fontSize!) : tabBarDefaultFontSize
196164

197-
var attributes = createFontAttributes(
198-
size: fontSize,
199-
family: props.fontFamily,
200-
weight: props.fontWeight,
201-
inactiveTintColor: props.inactiveTintColor
165+
let attributes = TabBarFontSize.createNormalStateAttributes(
166+
fontSize: props.fontSize,
167+
fontFamily: props.fontFamily,
168+
fontWeight: props.fontWeight,
169+
inactiveColor: props.inactiveTintColor
202170
)
203171

204-
if let inactiveTintColor = props.inactiveTintColor {
205-
attributes[.foregroundColor] = inactiveTintColor
206-
}
207-
208172
if let inactiveTintColor = props.inactiveTintColor {
209173
itemAppearance.normal.iconColor = inactiveTintColor
210174
}

0 commit comments

Comments
 (0)