From 8ec3a0eed45c821a7152039f62cf0bf3c45a7f68 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Thu, 10 Oct 2024 21:35:52 +0800 Subject: [PATCH] feat(ios): add HippyFontChangeTriggerNotification for native font update --- .../native/ios/renderer/HippyComponentMap.h | 23 +++- .../native/ios/renderer/HippyComponentMap.mm | 4 + renderer/native/ios/renderer/HippyFont.h | 10 +- renderer/native/ios/renderer/HippyFont.mm | 28 ++--- renderer/native/ios/renderer/HippyUIManager.h | 9 ++ .../native/ios/renderer/HippyUIManager.mm | 54 ++++++++ .../component/text/HippyShadowText.mm | 3 +- .../component/textinput/HippyBaseTextInput.h | 9 ++ .../component/textinput/HippyBaseTextInput.m | 53 ++++++++ .../component/textinput/HippyShadowTextView.h | 11 +- .../textinput/HippyShadowTextView.mm | 115 ++++++++++++++++-- .../textinput/HippyTextViewManager.mm | 66 ++-------- 12 files changed, 294 insertions(+), 91 deletions(-) diff --git a/renderer/native/ios/renderer/HippyComponentMap.h b/renderer/native/ios/renderer/HippyComponentMap.h index c44552530ad..a24b2e60a5f 100644 --- a/renderer/native/ios/renderer/HippyComponentMap.h +++ b/renderer/native/ios/renderer/HippyComponentMap.h @@ -21,9 +21,7 @@ */ #import - #import "HippyComponent.h" - #include namespace hippy { @@ -39,6 +37,7 @@ typedef NS_ENUM(NSUInteger, HippyComponentReferenceType) { HippyComponentReferenceTypeWeak, }; +/// A structure that holds all root and components information @interface HippyComponentMap : NSObject /// Whether all recorded elements are strongly referenced, @@ -62,18 +61,30 @@ typedef NS_ENUM(NSUInteger, HippyComponentReferenceType) { rootNode:(std::weak_ptr)rootNode forTag:(NSNumber *)tag; +/// Remove root component +/// - Parameter tag: hippyTag - (void)removeRootComponentWithTag:(NSNumber *)tag; +/// Whether contain root component with given tag +/// - Parameter tag: hippyTag - (BOOL)containRootComponentWithTag:(NSNumber *)tag; +/// Get all rootTags +- (NSArray *)allRootTags; + +/// Get all root components - (NSArray> *)rootComponents; +/// Get root component with given tag +/// - Parameter tag: hippyTag - (__kindof id)rootComponentForTag:(NSNumber *)tag; +/// Get RootNode for given tag +/// - Parameter tag: hippyTag - (std::weak_ptr)rootNodeForTag:(NSNumber *)tag; -#pragma mark - +#pragma mark - Components manage /// Add a component to ComponentMap - (void)addComponent:(__kindof id)component forRootTag:(NSNumber *)tag; @@ -81,8 +92,14 @@ typedef NS_ENUM(NSUInteger, HippyComponentReferenceType) { /// Remove one component from ComponentMap - (void)removeComponent:(__kindof id)component forRootTag:(NSNumber *)tag; +/// Get all components for given root tag +/// - Parameter tag: hippyTag - (NSDictionary> *)componentsForRootTag:(NSNumber *)tag; +/// Get specific component on rootTag with given tag +/// - Parameters: +/// - componentTag: hippyTag +/// - tag: rootTag - (__kindof id)componentForTag:(NSNumber *)componentTag onRootTag:(NSNumber *)tag; diff --git a/renderer/native/ios/renderer/HippyComponentMap.mm b/renderer/native/ios/renderer/HippyComponentMap.mm index 6ff1912826f..e042d0aa10c 100644 --- a/renderer/native/ios/renderer/HippyComponentMap.mm +++ b/renderer/native/ios/renderer/HippyComponentMap.mm @@ -89,6 +89,10 @@ - (BOOL)containRootComponentWithTag:(NSNumber *)tag { return nil != rootComponent; } +- (NSArray *)allRootTags { + return [[_rootComponentsMap keyEnumerator] allObjects]; +} + - (NSArray> *)rootComponents { return [[_rootComponentsMap objectEnumerator] allObjects]; } diff --git a/renderer/native/ios/renderer/HippyFont.h b/renderer/native/ios/renderer/HippyFont.h index 1950555f33a..f120b8f779b 100644 --- a/renderer/native/ios/renderer/HippyFont.h +++ b/renderer/native/ios/renderer/HippyFont.h @@ -39,10 +39,12 @@ variant:(NSArray *)variant scaleMultiplier:(CGFloat)scaleMultiplier; -+ (UIFont *)updateFont:(UIFont *)font withFamily:(NSString *)family; -+ (UIFont *)updateFont:(UIFont *)font withSize:(NSNumber *)size; -+ (UIFont *)updateFont:(UIFont *)font withWeight:(NSString *)weight; -+ (UIFont *)updateFont:(UIFont *)font withStyle:(NSString *)style; +/// Get the +/// JS side usually pass a `fontName` instead of `fontFamily` +/// If not match, the original value is returned. +/// +/// - Parameter fontFamily: NSString * ++ (NSString *)familyNameWithCSSNameMatching:(NSString *)fontName; @end diff --git a/renderer/native/ios/renderer/HippyFont.mm b/renderer/native/ios/renderer/HippyFont.mm index 258215c8756..ef945ca6f13 100644 --- a/renderer/native/ios/renderer/HippyFont.mm +++ b/renderer/native/ios/renderer/HippyFont.mm @@ -115,7 +115,9 @@ @implementation HippyConvert (NativeRenderFont) + (UIFont *)UIFont:(id)json { json = [self NSDictionary:json]; - return [HippyFont updateFont:nil withFamily:[HippyConvert NSString:json[@"fontFamily"]] size:[HippyConvert NSNumber:json[@"fontSize"]] + return [HippyFont updateFont:nil + withFamily:[HippyConvert NSString:json[@"fontFamily"]] + size:[HippyConvert NSNumber:json[@"fontSize"]] weight:[HippyConvert NSString:json[@"fontWeight"]] style:[HippyConvert NSString:json[@"fontStyle"]] variant:[HippyConvert NSStringArray:json[@"fontVariant"]] @@ -322,20 +324,16 @@ + (UIFont *)updateFont:(UIFont *)font return font; } -+ (UIFont *)updateFont:(UIFont *)font withFamily:(NSString *)family { - return [self updateFont:font withFamily:family size:nil weight:nil style:nil variant:nil scaleMultiplier:1]; -} - -+ (UIFont *)updateFont:(UIFont *)font withSize:(NSNumber *)size { - return [self updateFont:font withFamily:nil size:size weight:nil style:nil variant:nil scaleMultiplier:1]; -} - -+ (UIFont *)updateFont:(UIFont *)font withWeight:(NSString *)weight { - return [self updateFont:font withFamily:nil size:nil weight:weight style:nil variant:nil scaleMultiplier:1]; -} - -+ (UIFont *)updateFont:(UIFont *)font withStyle:(NSString *)style { - return [self updateFont:font withFamily:nil size:nil weight:nil style:style variant:nil scaleMultiplier:1]; ++ (NSString *)familyNameWithCSSNameMatching:(NSString *)fontName { + NSString *familyName = fontName; + if (fontName && ![[UIFont familyNames] containsObject:fontName]) { + // Not a real FamilyName + // Using CSS name matching semantics. + // fontSize here is just a placeholder for getting font. + UIFont *cssFont = [UIFont fontWithName:fontName size:14.0]; + familyName = cssFont.familyName; + } + return familyName; } @end diff --git a/renderer/native/ios/renderer/HippyUIManager.h b/renderer/native/ios/renderer/HippyUIManager.h index f39449098ec..f4d03aa4aee 100644 --- a/renderer/native/ios/renderer/HippyUIManager.h +++ b/renderer/native/ios/renderer/HippyUIManager.h @@ -62,6 +62,15 @@ HIPPY_EXTERN NSString *const HippyUIManagerRootViewTagKey; */ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification; +/** + * This notification can be sent when the font is registered or modified on the native side + * and hippy needs to be refreshed. + * + * `notification.object` can carry rootTag to filter the RootView that needs to be refreshed. + * Default value nil indicating that a refresh is required. + */ +HIPPY_EXTERN NSString *const HippyFontChangeTriggerNotification; + /// The HippyUIManager responsible for updating the view hierarchy. diff --git a/renderer/native/ios/renderer/HippyUIManager.mm b/renderer/native/ios/renderer/HippyUIManager.mm index b6750e43e87..c24a2d3df9d 100644 --- a/renderer/native/ios/renderer/HippyUIManager.mm +++ b/renderer/native/ios/renderer/HippyUIManager.mm @@ -49,6 +49,8 @@ #import "HippyModuleMethod.h" #import "HippyBridge+Private.h" #import "HippyJSExecutor.h" +#import "HippyShadowText.h" +#import "HippyShadowTextView.h" #import "dom/root_node.h" #import "objc/runtime.h" #import @@ -181,6 +183,7 @@ - (void)setUiManager:(HippyUIManager *)uiManager; NSString *const HippyUIManagerRootViewKey = @"HippyUIManagerRootViewKey"; NSString *const HippyUIManagerRootViewTagKey = @"HippyUIManagerRootViewTagKey"; NSString *const HippyUIManagerDidEndBatchNotification = @"HippyUIManagerDidEndBatchNotification"; +NSString *const HippyFontChangeTriggerNotification = @"HippyFontChangeTriggerNotification"; @interface HippyUIManager() { NSMutableArray *_pendingUIBlocks; @@ -233,6 +236,10 @@ - (void)initContext { _componentDataLock = OS_UNFAIR_LOCK_INIT; HippyScreenScale(); HippyScreenSize(); + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(onFontChangedFromNative:) + name:HippyFontChangeTriggerNotification + object:nil]; } - (void)invalidate { @@ -1484,6 +1491,53 @@ - (void)domEventDidHandle:(const std::string &)eventName forNode:(int32_t)tag on // no op } + +#pragma mark - Font Refresh + +- (void)onFontChangedFromNative:(NSNotification *)notification { + NSNumber *targetRootTag = notification.object; + if ((targetRootTag != nil) && ![self.viewRegistry containRootComponentWithTag:targetRootTag]) { + // do compare if notification has target RootView. + return; + } + + __weak __typeof(self)weakSelf = self; + [self.bridge.javaScriptExecutor executeAsyncBlockOnJavaScriptQueue:^{ + __strong __typeof(weakSelf)strongSelf = weakSelf; + if (!strongSelf) { + return; + } + + NSArray *allRootTags; + if (targetRootTag != nil) { + allRootTags = [NSArray arrayWithObject:targetRootTag]; + } else { + // UIManager may have more than one Root + allRootTags = strongSelf->_shadowViewRegistry.allRootTags; + } + + for (NSNumber *rootTag in allRootTags) { + NSArray *shadowViews = [strongSelf->_shadowViewRegistry componentsForRootTag:rootTag].allValues; + Class shadowTextClass = [HippyShadowText class]; + Class shadowTextViewClass = [HippyShadowTextView class]; + for (HippyShadowView *shadowView in shadowViews) { + if ([shadowView isKindOfClass:shadowTextClass] || + [shadowView isKindOfClass:shadowTextViewClass]) { + [shadowView dirtyText:NO]; + [shadowView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied]; + } + } + // do layout and refresh UI + auto domManager = self.domManager.lock(); + if (domManager) { + auto rootNode = [strongSelf->_shadowViewRegistry rootNodeForTag:rootTag]; + domManager->DoLayout(rootNode); + domManager->EndBatch(rootNode); + } + } + }]; +} + @end diff --git a/renderer/native/ios/renderer/component/text/HippyShadowText.mm b/renderer/native/ios/renderer/component/text/HippyShadowText.mm index f1fdc56bfb2..cdf95d7cbb6 100644 --- a/renderer/native/ios/renderer/component/text/HippyShadowText.mm +++ b/renderer/native/ios/renderer/component/text/HippyShadowText.mm @@ -418,8 +418,7 @@ - (void)dirtyText:(BOOL)needToDoLayout { BOOL isJSTaskRunner = (domManager->GetTaskRunner() && footstone::TaskRunner::GetCurrentTaskRunner()); if (isJSTaskRunner) { domNodeAction(); - } - else { + } else { std::vector> ops = {domNodeAction}; domManager->PostTask(hippy::dom::Scene(std::move(ops))); } diff --git a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h index 24816e20704..5b7ac631163 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h +++ b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.h @@ -24,6 +24,15 @@ @interface HippyBaseTextInput : HippyView +/// Font property - FontSize +@property (nonatomic, strong) NSNumber *fontSize; +/// Font property - FontWeight +@property (nonatomic, strong) NSString *fontWeight; +/// Font property - FontStyle +@property (nonatomic, strong) NSString *fontStyle; +/// Font property - FontFamily +@property (nonatomic, strong) NSString *fontFamily; + @property (nonatomic, strong) UIFont *font; @property (nonatomic, assign) UIEdgeInsets contentInset; @property (nonatomic, copy) NSString *value; diff --git a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m index 3559c79205e..ff622e02055 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m +++ b/renderer/native/ios/renderer/component/textinput/HippyBaseTextInput.m @@ -21,6 +21,7 @@ */ #import "HippyBaseTextInput.h" +#import "HippyFont.h" static NSString *const kKeyboardHeightKey = @"keyboardHeight"; @@ -95,4 +96,56 @@ - (void)setOnKeyboardHeightChanged:(HippyDirectEventBlock)onKeyboardHeightChange } } + +#pragma mark - Hippy Update Callback + +- (void)hippyBridgeDidFinishTransaction { + // Use this opportunity to update font if needed. + [self layoutIfNeeded]; +} + +- (void)layoutSubviews { + [super layoutSubviews]; + [self rebuildAndUpdateFont]; +} + + +#pragma mark - Font Related + +- (void)setFontSize:(NSNumber *)fontSize { + _fontSize = fontSize; + [self setNeedsLayout]; +} + +- (void)setFontStyle:(NSString *)fontStyle { + _fontStyle = fontStyle; + [self setNeedsLayout]; +} + +- (void)setFontWeight:(NSString *)fontWeight { + _fontWeight = fontWeight; + [self setNeedsLayout]; +} + +- (void)setFontFamily:(NSString *)fontFamily { + _fontFamily = fontFamily; + [self setNeedsLayout]; +} + +- (void)rebuildAndUpdateFont { + // Convert fontName to fontFamily if needed + CGFloat scaleMultiplier = 1.0; // scale not supported + NSString *familyName = [HippyFont familyNameWithCSSNameMatching:self.fontFamily]; + UIFont *font = [HippyFont updateFont:self.font + withFamily:familyName + size:self.fontSize + weight:self.fontWeight + style:self.fontStyle + variant:nil + scaleMultiplier:scaleMultiplier]; + if (self.font != font) { + self.font = font; + } +} + @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.h b/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.h index 412fc6bb2d6..7648e2a6bab 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.h +++ b/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.h @@ -27,8 +27,6 @@ @property (nonatomic, copy) NSString *text; @property (nonatomic, copy) NSString *placeholder; -@property (nonatomic, strong) UIFont *font; - /// ParagraphStyles - lineHeight @property (nonatomic, strong) NSNumber *lineHeight; /// ParagraphStyles - lineSpacing @@ -36,4 +34,13 @@ /// ParagraphStyles - lineHeightMultiple @property (nonatomic, strong) NSNumber *lineHeightMultiple; +/// Font property - FontSize +@property (nonatomic, strong) NSNumber *fontSize; +/// Font property - FontWeight +@property (nonatomic, strong) NSString *fontWeight; +/// Font property - FontStyle +@property (nonatomic, strong) NSString *fontStyle; +/// Font property - FontFamily +@property (nonatomic, strong) NSString *fontFamily; + @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.mm b/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.mm index ab14107a402..83b518f50e1 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.mm +++ b/renderer/native/ios/renderer/component/textinput/HippyShadowTextView.mm @@ -23,6 +23,7 @@ #import "HippyUtils.h" #import "HippyShadowTextView.h" #import "HippyShadowView+Internal.h" +#import "HippyFont.h" #include "dom/dom_manager.h" #include "dom/dom_node.h" #include "dom/layout_node.h" @@ -34,9 +35,16 @@ @interface HippyShadowTextView () +/// Cached font +@property (nonatomic, strong) UIFont *font; +/// Whether font needs to be updated. +@property (nonatomic, assign) BOOL isFontDirty; /// Cached text attributes @property (nonatomic, strong) NSDictionary *dicAttributes; +/// rebuild and update the font property +- (void)rebuildAndUpdateFont; + @end static hippy::LayoutSize x5MeasureFunc( @@ -48,8 +56,12 @@ @interface HippyShadowTextView () HippyShadowTextView *shadowText = weakShadowText; NSString *text = shadowText.text ?: shadowText.placeholder; if (nil == shadowText.dicAttributes) { + if (shadowText.isFontDirty) { + [shadowText rebuildAndUpdateFont]; + shadowText.isFontDirty = NO; + } + // Keep this historical code, default fontSize 16. if (shadowText.font == nil) { - shadowText.font = [UIFont systemFontOfSize:defaultFontSize]; } NSDictionary *attrs = nil; @@ -82,13 +94,6 @@ @interface HippyShadowTextView () @implementation HippyShadowTextView -- (instancetype)init { - self = [super init]; - if (self) { - } - return self; -} - - (void)setDomManager:(std::weak_ptr)domManager { [super setDomManager:domManager]; auto shared_domNode = domManager.lock(); @@ -145,4 +150,98 @@ - (void)didUpdateHippySubviews { } } +- (void)dirtyText:(BOOL)needToDoLayout { + [super dirtyText:needToDoLayout]; + self.isFontDirty = YES; + self.dicAttributes = nil; + + // mark layout node dirty + auto domManager = self.domManager.lock(); + auto weakDomManager = self.domManager; + if (domManager) { + __weak HippyShadowView *weakSelf = self; + auto domNodeAction = [needToDoLayout, weakSelf, weakDomManager](){ + @autoreleasepool { + HippyShadowView *strongSelf = weakSelf; + if (!strongSelf) { + return; + } + auto strongDomManager = weakDomManager.lock(); + if (!strongDomManager) { + return; + } + int32_t componentTag = [[strongSelf hippyTag] intValue]; + auto domNode = strongDomManager->GetNode(strongSelf.rootNode, componentTag); + if (domNode) { + domNode->GetLayoutNode()->MarkDirty(); + if (needToDoLayout) { + strongDomManager->DoLayout(strongSelf.rootNode); + strongDomManager->EndBatch(strongSelf.rootNode); + } + } + } + }; + BOOL isJSTaskRunner = (domManager->GetTaskRunner() && footstone::TaskRunner::GetCurrentTaskRunner()); + if (isJSTaskRunner) { + domNodeAction(); + } else { + std::vector> ops = {domNodeAction}; + domManager->PostTask(hippy::dom::Scene(std::move(ops))); + } + } +} + +- (void)amendLayoutBeforeMount:(NSMutableSet *)blocks { + [super amendLayoutBeforeMount:blocks]; + + if (NativeRenderUpdateLifecycleComputed == _propagationLifecycle) { + return; + } + //Set needs layout for font change event, etc. + NSNumber *currentTag = self.hippyTag; + [blocks addObject:^(NSDictionary *viewRegistry, UIView * _Nullable lazyCreatedView) { + UIView *view = lazyCreatedView ?: viewRegistry[currentTag]; + [view setNeedsLayout]; + }]; +} + + +#pragma mark - Font Related + +- (void)setFontSize:(NSNumber *)fontSize { + _fontSize = fontSize; + self.isFontDirty = YES; +} + +- (void)setFontStyle:(NSString *)fontStyle { + _fontStyle = fontStyle; + self.isFontDirty = YES; +} + +- (void)setFontWeight:(NSString *)fontWeight { + _fontWeight = fontWeight; + self.isFontDirty = YES; +} + +- (void)setFontFamily:(NSString *)fontFamily { + _fontFamily = fontFamily; + self.isFontDirty = YES; +} + +- (void)rebuildAndUpdateFont { + // Convert fontName to fontFamily if needed + CGFloat scaleMultiplier = 1.0; // scale not supported + NSString *familyName = [HippyFont familyNameWithCSSNameMatching:self.fontFamily]; + UIFont *font = [HippyFont updateFont:self.font + withFamily:familyName + size:self.fontSize + weight:self.fontWeight + style:self.fontStyle + variant:nil + scaleMultiplier:scaleMultiplier]; + if (self.font != font) { + self.font = font; + } +} + @end diff --git a/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm b/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm index 518d46d6c61..7b9bfbf3f1c 100644 --- a/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm +++ b/renderer/native/ios/renderer/component/textinput/HippyTextViewManager.mm @@ -140,6 +140,11 @@ - (HippyShadowView *)shadowView { HIPPY_EXPORT_SHADOW_PROPERTY(lineSpacing, NSNumber) HIPPY_EXPORT_SHADOW_PROPERTY(lineHeightMultiple, NSNumber) +HIPPY_EXPORT_SHADOW_PROPERTY(fontSize, NSNumber) +HIPPY_EXPORT_SHADOW_PROPERTY(fontWeight, NSString) +HIPPY_EXPORT_SHADOW_PROPERTY(fontStyle, NSString) +HIPPY_EXPORT_SHADOW_PROPERTY(fontFamily, NSString) + HIPPY_EXPORT_VIEW_PROPERTY(lineHeight, NSNumber) HIPPY_EXPORT_VIEW_PROPERTY(lineSpacing, NSNumber) HIPPY_EXPORT_VIEW_PROPERTY(lineHeightMultiple, NSNumber) @@ -168,44 +173,10 @@ - (HippyShadowView *)shadowView { HIPPY_EXPORT_VIEW_PROPERTY(text, NSString) HIPPY_REMAP_VIEW_PROPERTY(caretColor, textView.caretColor, UIColor) -HIPPY_CUSTOM_SHADOW_PROPERTY(fontSize, NSNumber, HippyShadowTextView) { - view.font = [HippyFont updateFont:view.font withSize:json]; -} - -HIPPY_CUSTOM_SHADOW_PROPERTY(fontWeight, NSString, HippyShadowTextView) { - view.font = [HippyFont updateFont:view.font withWeight:json]; -} - -HIPPY_CUSTOM_SHADOW_PROPERTY(fontStyle, NSString, HippyShadowTextView) { - view.font = [HippyFont updateFont:view.font withStyle:json]; // defaults to normal -} - -HIPPY_CUSTOM_SHADOW_PROPERTY(fontFamily, NSString, HippyShadowTextView) { - // 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) { - // Convert fontName to fontFamily if needed - NSString *familyName = [self familyNameWithCSSNameMatching:json]; - view.font = [HippyFont updateFont:view.font withFamily:familyName ?: defaultView.font.familyName]; -} +HIPPY_EXPORT_VIEW_PROPERTY(fontSize, NSNumber) +HIPPY_EXPORT_VIEW_PROPERTY(fontWeight, NSString) +HIPPY_EXPORT_VIEW_PROPERTY(fontStyle, NSString) +HIPPY_EXPORT_VIEW_PROPERTY(fontFamily, NSString) - (HippyViewManagerUIBlock)uiBlockToAmendWithShadowView:(HippyShadowView *)hippyShadowView { NSNumber *componentTag = hippyShadowView.hippyTag; @@ -215,23 +186,4 @@ - (HippyViewManagerUIBlock)uiBlockToAmendWithShadowView:(HippyShadowView *)hippy }; } - -#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