Skip to content

Commit 9a8bc85

Browse files
fix(ios): accept the ui-monospace font family
React Native's Fabric text maps the CSS generic family `ui-monospace` to the system monospaced font (SF Mono), but the legacy `RCTFont` this library uses does not know it and falls back to the proportional system font. Code spans and code blocks styled with `fontFamily: "ui-monospace"` rendered in SF Pro. `ENRMFont` wraps `RCTFont updateFont:` and resolves `ui-monospace` to the system font with the monospaced design, keeping weight, italics and scaling. It also recognises a restyled run (bold or italic inside code) that passes its current SF Mono font with no family, whose hidden family name RCTFont would not resolve.
1 parent 3757a44 commit 9a8bc85

8 files changed

Lines changed: 186 additions & 133 deletions

File tree

‎packages/react-native-enriched-markdown/ios/renderer/CodeRenderer.m‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ - (void)renderNodeContent:(MarkdownASTNode *)node
3333
UIFont *codeFont;
3434
if (codeFontFamily.length > 0) {
3535
NSString *weightStr = (weight == UIFontWeightBold) ? @"bold" : nil;
36-
codeFont = [RCTFont updateFont:nil
37-
withFamily:codeFontFamily
38-
size:@(codeFontSize)
39-
weight:weightStr
40-
style:nil
41-
variant:nil
42-
scaleMultiplier:fontScale];
36+
codeFont = [ENRMFont updateFont:nil
37+
withFamily:codeFontFamily
38+
size:@(codeFontSize)
39+
weight:weightStr
40+
style:nil
41+
variant:nil
42+
scaleMultiplier:fontScale];
4343
} else {
4444
codeFont = [UIFont monospacedSystemFontOfSize:codeFontSize * fontScale weight:weight];
4545
}

‎packages/react-native-enriched-markdown/ios/renderer/EmphasisRenderer.m‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ - (void)renderNodeContent:(MarkdownASTNode *)node
4040
UIFont *resolvedFont = nil;
4141

4242
if (emphasisFontFamily.length > 0) {
43-
resolvedFont = [RCTFont updateFont:currentFont
44-
withFamily:emphasisFontFamily
45-
size:nil
46-
weight:nil
47-
style:useNormalStyle ? nil : @"italic"
48-
variant:nil
49-
scaleMultiplier:1.0];
43+
resolvedFont = [ENRMFont updateFont:currentFont
44+
withFamily:emphasisFontFamily
45+
size:nil
46+
weight:nil
47+
style:useNormalStyle ? nil : @"italic"
48+
variant:nil
49+
scaleMultiplier:1.0];
5050
} else if (currentFont &&
5151
!(currentFont.fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic)) {
5252
resolvedFont = [self ensureFontIsItalic:currentFont];

‎packages/react-native-enriched-markdown/ios/renderer/LinkRenderer.m‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ - (void)renderNodeContent:(MarkdownASTNode *)node
139139
if (linkFontFamily.length > 0) {
140140
UIFont *currentFont = attrs[NSFontAttributeName];
141141
if (currentFont) {
142-
UIFont *linkFont = [RCTFont updateFont:currentFont
143-
withFamily:linkFontFamily
144-
size:nil
145-
weight:nil
146-
style:nil
147-
variant:nil
148-
scaleMultiplier:1.0];
142+
UIFont *linkFont = [ENRMFont updateFont:currentFont
143+
withFamily:linkFontFamily
144+
size:nil
145+
weight:nil
146+
style:nil
147+
variant:nil
148+
scaleMultiplier:1.0];
149149
if (linkFont && ![currentFont isEqual:linkFont]) {
150150
newAttributes[NSFontAttributeName] = linkFont;
151151
}

‎packages/react-native-enriched-markdown/ios/renderer/RenderContext.m‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ - (UIFont *)cachedFontForSize:(CGFloat)fontSize family:(NSString *)fontFamily we
9090

9191
NSString *effectiveWeight = (fontWeight.length > 0) ? fontWeight : nil;
9292

93-
UIFont *font = [RCTFont updateFont:nil
94-
withFamily:fontFamily
95-
size:@(fontSize)
96-
weight:effectiveWeight
97-
style:nil
98-
variant:nil
99-
scaleMultiplier:effectiveMultiplier];
93+
UIFont *font = [ENRMFont updateFont:nil
94+
withFamily:fontFamily
95+
size:@(fontSize)
96+
weight:effectiveWeight
97+
style:nil
98+
variant:nil
99+
scaleMultiplier:effectiveMultiplier];
100100

101101
if (font) {
102102
_fontCache[key] = font;

‎packages/react-native-enriched-markdown/ios/renderer/StrongRenderer.m‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ - (void)renderNodeContent:(MarkdownASTNode *)node
4040

4141
if (strongFontFamily.length > 0) {
4242
NSString *weight = useNormalWeight ? nil : @"bold";
43-
resolvedFont = [RCTFont updateFont:currentFont
44-
withFamily:strongFontFamily
45-
size:nil
46-
weight:weight
47-
style:nil
48-
variant:nil
49-
scaleMultiplier:1.0];
43+
resolvedFont = [ENRMFont updateFont:currentFont
44+
withFamily:strongFontFamily
45+
size:nil
46+
weight:weight
47+
style:nil
48+
variant:nil
49+
scaleMultiplier:1.0];
5050
} else if (!([currentFont.fontDescriptor symbolicTraits] & UIFontDescriptorTraitBold)) {
5151
resolvedFont = [self ensureFontIsBold:currentFont];
5252
}

0 commit comments

Comments
 (0)