Skip to content

Commit

Permalink
fix(ios): add CSS name matching semantics to fontFamily of TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Jun 14, 2024
1 parent 403af1a commit b6697c5
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,30 @@ - (HippyShadowView *)shadowView {
}

HIPPY_CUSTOM_SHADOW_PROPERTY(fontFamily, NSString, HippyShadowTextView) {
view.font = [HippyFont updateFont:view.font withFamily:json];
// Convert fontName to fontFamily if needed
NSString *familyName = [self familyNameWithCSSNameMatching:json];
view.font = [HippyFont updateFont:view.font withFamily:familyName];
}

HIPPY_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, HippyBaseTextInput) {
UIFont *theFont = [HippyFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
view.font = theFont;
}

HIPPY_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused HippyBaseTextInput) {
UIFont *theFont = [HippyFont updateFont:view.font withWeight:json]; // defaults to normal
view.font = theFont;
}

HIPPY_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused HippyBaseTextInput) {
UIFont *theFont = [HippyFont updateFont:view.font withStyle:json];
view.font = theFont; // defaults to normal
}

HIPPY_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, HippyBaseTextInput) {
view.font = [HippyFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
// Convert fontName to fontFamily if needed
NSString *familyName = [self familyNameWithCSSNameMatching:json];
view.font = [HippyFont updateFont:view.font withFamily:familyName ?: defaultView.font.familyName];
}

- (HippyViewManagerUIBlock)uiBlockToAmendWithShadowView:(HippyShadowView *)hippyShadowView {
Expand All @@ -211,4 +218,24 @@ - (HippyViewManagerUIBlock)uiBlockToAmendWithShadowView:(HippyShadowView *)hippy
viewRegistry[componentTag].contentInset = padding;
};
}


#pragma mark - Private

/// Get the
/// JS side usually pass a `fontName` instead of `fontFamily`
/// - Parameter json: id
- (NSString *)familyNameWithCSSNameMatching:(id)json {
NSString *familyName = json;
if (json && ![[UIFont familyNames] containsObject:json]) {
// Not a real FamilyName
// Using CSS name matching semantics.
// fontSize here is just a placeholder for getting font.
UIFont *cssFont = [UIFont fontWithName:json size:14.0];
familyName = cssFont.familyName;
}
return familyName;
}


@end

0 comments on commit b6697c5

Please sign in to comment.