-
Notifications
You must be signed in to change notification settings - Fork 70
fix(input): derive heading bold from the base font on iOS #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,8 +112,22 @@ - (UIFont *)headingFontForLevel:(NSInteger)level | |
| } | ||
|
|
||
| NSString *weightString = _headingFontWeights[level]; | ||
| UIFont *font = weightString.length > 0 ? [UIFont systemFontOfSize:size weight:ENRMFontWeightFromString(weightString)] | ||
| : [_baseFont fontWithSize:size]; | ||
| UIFont *font; | ||
| if (weightString.length > 0 && ENRMFontWeightFromString(weightString) >= UIFontWeightBold) { | ||
| // Derive the bold face of the base (content) font β matching Android's | ||
| // InputHeadingSpan, which folds heading weight into the current typeface β | ||
| // so a custom editor font keeps its family in headings. Fall back to the | ||
| // system font only when the family has no bold face. | ||
| UIFont *sized = [_baseFont fontWithSize:size]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick - Two small things in this hunk:
UIFontDescriptor *descriptor = [_baseFont.fontDescriptor
fontDescriptorWithSymbolicTraits:_baseFont.fontDescriptor.symbolicTraits | UIFontDescriptorTraitBold];
UIFontWeight weight = ENRMFontWeightFromString(weightString);
if (weightString.length > 0 && weight >= UIFontWeightBold) {
...
} else if (weightString.length > 0) {
font = [UIFont systemFontOfSize:size weight:weight];
}Non-blocking β and if we go with the UIFontWeightTrait approach from the other |
||
| UIFontDescriptor *descriptor = [sized.fontDescriptor | ||
| fontDescriptorWithSymbolicTraits:sized.fontDescriptor.symbolicTraits | UIFontDescriptorTraitBold]; | ||
| font = descriptor ? [UIFont fontWithDescriptor:descriptor size:size] | ||
| : [UIFont systemFontOfSize:size weight:UIFontWeightBold]; | ||
| } else if (weightString.length > 0) { | ||
| font = [UIFont systemFontOfSize:size weight:ENRMFontWeightFromString(weightString)]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new bold branch correctly derives the heading font from the base font's family, but the
This is the same divergence this PR fixes for bold, just for the remaining weights β and the family may well have matching faces (Arial Light/Medium/etc.) we could use. Suggestion: instead of special-casing bold via the symbolic trait, derive every non-empty weight from the base font's descriptor using the weight trait, e.g. UIFontDescriptor *descriptor = [_baseFont.fontDescriptor fontDescriptorByAddingAttributes:@{
UIFontDescriptorTraitsAttribute : @{UIFontWeightTrait : @(weight)}
}];The system picks the closest face in the family to the requested weight, which would collapse the bold branch and this one into a single path (keeping the system-font fallback for families where descriptor resolution fails). |
||
| } else { | ||
| font = [_baseFont fontWithSize:size]; | ||
| } | ||
| _headingFontCache[level] = font; | ||
| return font; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is correct, I've checked that
ENRMFontWeightFromStringis incomplete if we use it for this comparasion; Because the mapping returnsUIFontWeightRegular (0.0)for "800", "900", "heavy", and "black", those weights arrive at line 116 as0.0, fail the>= 0.4check, and skip the new bold-derivation branch β even though semantically they are bolder than bold.If we want to use this function in this way we would require to fix the
ENRMFontWeightFromStringto mirror RN's own RCTFont table:100,ultralightUIFontWeightUltraLight200,thinUIFontWeightThin300,lightUIFontWeightLight400,normal,regularUIFontWeightRegular500,mediumUIFontWeightMedium600,semiboldUIFontWeightSemibold700,boldUIFontWeightBold800,heavyUIFontWeightHeavy900,blackUIFontWeightBlackUIFontWeightRegular