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 12, 2024
1 parent 946baa9 commit 55b5eea
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions ios/sdk/component/textinput/HippyTextViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,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 *)shadowView {
Expand All @@ -228,4 +235,24 @@ - (HippyViewManagerUIBlock)uiBlockToAmendWithShadowView:(HippyShadowView *)shado
viewRegistry[hippyTag].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 55b5eea

Please sign in to comment.