|
| 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 | + |
| 83 | + /// Validates if a font family is available on the current platform |
| 84 | + /// - Parameter family: Font family name to validate |
| 85 | + /// - Returns: True if font family is available |
| 86 | + static func isFontFamilyAvailable(_ family: String) -> Bool { |
| 87 | + #if os(macOS) |
| 88 | + return NSFontManager.shared.availableFontFamilies.contains(family) |
| 89 | + #else |
| 90 | + return UIFont.familyNames.contains(family) |
| 91 | + #endif |
| 92 | + } |
| 93 | + |
| 94 | + /// Maps font weight strings to platform-specific font weights |
| 95 | + /// - Parameter weight: Font weight string (e.g., "bold", "normal", "100"-"900") |
| 96 | + /// - Returns: Appropriate font weight for the platform |
| 97 | + static func mapFontWeight(_ weight: String?) -> Any? { |
| 98 | + guard let weight = weight?.lowercased() else { return nil } |
| 99 | + |
| 100 | + #if os(macOS) |
| 101 | + switch weight { |
| 102 | + case "100", "ultralight": return NSFont.Weight.ultraLight |
| 103 | + case "200", "thin": return NSFont.Weight.thin |
| 104 | + case "300", "light": return NSFont.Weight.light |
| 105 | + case "400", "normal", "regular": return NSFont.Weight.regular |
| 106 | + case "500", "medium": return NSFont.Weight.medium |
| 107 | + case "600", "semibold": return NSFont.Weight.semibold |
| 108 | + case "700", "bold": return NSFont.Weight.bold |
| 109 | + case "800", "heavy": return NSFont.Weight.heavy |
| 110 | + case "900", "black": return NSFont.Weight.black |
| 111 | + default: return NSFont.Weight.regular |
| 112 | + } |
| 113 | + #else |
| 114 | + switch weight { |
| 115 | + case "100", "ultralight": return UIFont.Weight.ultraLight |
| 116 | + case "200", "thin": return UIFont.Weight.thin |
| 117 | + case "300", "light": return UIFont.Weight.light |
| 118 | + case "400", "normal", "regular": return UIFont.Weight.regular |
| 119 | + case "500", "medium": return UIFont.Weight.medium |
| 120 | + case "600", "semibold": return UIFont.Weight.semibold |
| 121 | + case "700", "bold": return UIFont.Weight.bold |
| 122 | + case "800", "heavy": return UIFont.Weight.heavy |
| 123 | + case "900", "black": return UIFont.Weight.black |
| 124 | + default: return UIFont.Weight.regular |
| 125 | + } |
| 126 | + #endif |
| 127 | + } |
| 128 | +} |
0 commit comments