diff --git a/ios/sdk/component/text/HippyShadowText.mm b/ios/sdk/component/text/HippyShadowText.mm index 435bdc08f..7403c0f66 100644 --- a/ios/sdk/component/text/HippyShadowText.mm +++ b/ios/sdk/component/text/HippyShadowText.mm @@ -44,21 +44,27 @@ CGFloat const HippyTextAutoSizeGranularity = 0.001f; @implementation HippyShadowText // MTTlayout HPSize textMeasureFunc( - HPNodeRef node, float width, MeasureMode widthMeasureMode, __unused float height, __unused MeasureMode heightMeasureMode, void *layoutContext) { - hippy::DomNode *pNode = static_cast(HPNodeGetContext(node)); - NSDictionary *props = unorderedMapDomValueToDictionary(pNode->GetExtStyle()); - NSDictionary *styleProps = unorderedMapDomValueToDictionary(pNode->GetStyleMap()); - CGFloat fontSize = [styleProps[@"fontSize"] floatValue]; - NSString *text = props[@"text"]; - if (0 == fontSize) { - fontSize = [UIFont systemFontSize]; + HippyShadowText *shadowText, HPNodeRef node, float width, + MeasureMode widthMeasureMode, float height, + MeasureMode heightMeasureMode, void *layoutContext) { +// hippy::DomNode *pNode = static_cast(HPNodeGetContext(node)); + std::shared_ptr domNode = [shadowText domNode].lock(); + if (domNode) { + NSDictionary *props = unorderedMapDomValueToDictionary(domNode->GetExtStyle()); + NSDictionary *styleProps = unorderedMapDomValueToDictionary(domNode->GetStyleMap()); + CGFloat fontSize = [styleProps[@"fontSize"] floatValue]; + NSString *text = props[@"text"]; + if (0 == fontSize) { + fontSize = [UIFont systemFontSize]; + } + UIFont *font = [UIFont systemFontOfSize:fontSize]; + CGSize size = [text sizeWithFont:font]; + HPSize retSize; + retSize.width = HippyCeilPixelValue(size.width); + retSize.height = HippyCeilPixelValue(size.height); + return retSize; } - UIFont *font = [UIFont systemFontOfSize:fontSize]; - CGSize size = [text sizeWithFont:font]; - HPSize retSize; - retSize.width = HippyCeilPixelValue(size.width); - retSize.height = HippyCeilPixelValue(size.height); - return retSize; + return HPSize{0, 0}; } static void resetFontAttribute(NSTextStorage *textStorage) { @@ -353,11 +359,12 @@ static void resetFontAttribute(NSTextStorage *textStorage) { NSWritingDirection direction = [[HippyI18nUtils sharedInstance] writingDirectionForCurrentAppLanguage]; HPDirection nodeDirection = (NSWritingDirectionRightToLeft == direction) ? DirectionRTL : DirectionLTR; nodeDirection = self.layoutDirection != DirectionInherit ? self.layoutDirection : nodeDirection; -// HPNodeDoLayout(child.nodeRef, NAN, NAN, nodeDirection); - float width = child.domNode->GetLayoutResult().width; - //HPNodeLayoutGetWidth(child.nodeRef); - float height = child.domNode->GetLayoutResult().height; -// HPNodeLayoutGetHeight(child.nodeRef); + auto domNode = child.domNode.lock(); + float width = 0, height = 0; + if (domNode) { + width = domNode->GetLayoutResult().width; + height = domNode->GetLayoutResult().height; + } if (isnan(width) || isnan(height)) { HippyLogError(@"Views nested within a must have a width and height"); } @@ -684,6 +691,23 @@ HIPPY_TEXT_PROPERTY(TextShadowColor, _textShadowColor, UIColor *); return newProps; } +- (void)setDomNode:(std::weak_ptr)domNode { + [super setDomNode:domNode]; + std::shared_ptr node = domNode.lock(); + if (node) { + hippy::TaitankMeasureFunction measureFunc = + [shadow_view = self](HPNodeRef node, float width, + MeasureMode widthMeasureMode, float height, + MeasureMode heightMeasureMode, void *layoutContext){ + return textMeasureFunc(shadow_view, node, width, widthMeasureMode, + height, heightMeasureMode, layoutContext); + }; + std::shared_ptrlayoutNode = + std::static_pointer_cast(node->GetLayoutNode()); + layoutNode->SetMeasureFunction(measureFunc); + } +} + - (void)setText:(NSString *)text { double version = UIDevice.currentDevice.systemVersion.doubleValue; if (version >= 10.0 && version < 12.0) { diff --git a/ios/sdk/component/textinput/HippyShadowTextView.mm b/ios/sdk/component/textinput/HippyShadowTextView.mm index 184b12091..81fcc6227 100644 --- a/ios/sdk/component/textinput/HippyShadowTextView.mm +++ b/ios/sdk/component/textinput/HippyShadowTextView.mm @@ -30,8 +30,9 @@ @end static HPSize x5MeasureFunc( - HPNodeRef node, float width, MeasureMode widthMeasureMode, __unused float height, __unused MeasureMode heightMeasureMode, void *layoutContext) { - HippyShadowTextView *shadowText = (__bridge HippyShadowTextView *)node->getContext(); + HippyShadowTextView *shadowText, HPNodeRef node, + float width, MeasureMode widthMeasureMode, float height, + MeasureMode heightMeasureMode, void *layoutContext) { NSString *text = shadowText.text ?: shadowText.placeholder; if (nil == shadowText.dicAttributes) { if (shadowText.font == nil) { @@ -55,6 +56,23 @@ static HPSize x5MeasureFunc( return self; } +- (void)setDomNode:(std::weak_ptr)domNode { + [super setDomNode:domNode]; + std::shared_ptr node = domNode.lock(); + if (node) { + hippy::TaitankMeasureFunction measureFunc = + [shadow_view = self](HPNodeRef node, float width, + MeasureMode widthMeasureMode, float height, + MeasureMode heightMeasureMode, void *layoutContext){ + return x5MeasureFunc(shadow_view, node, width, widthMeasureMode, + height, heightMeasureMode, layoutContext); + }; + std::shared_ptrlayoutNode = + std::static_pointer_cast(node->GetLayoutNode()); + layoutNode->SetMeasureFunction(measureFunc); + } +} + /* * text类型控件会响应用户输入交互,但是并不会更新shadowText中的props属性, * 导致前端下发新的props属性与当前属性实际值不一致 diff --git a/ios/sdk/component/view/HippyShadowView.h b/ios/sdk/component/view/HippyShadowView.h index 9eb0f6eae..06398e1fa 100644 --- a/ios/sdk/component/view/HippyShadowView.h +++ b/ios/sdk/component/view/HippyShadowView.h @@ -75,7 +75,6 @@ typedef void (^HippyApplierVirtualBlock)(NSDictionary domNode; /** * isNewView - Used to track the first time the view is introduced into the hierarchy. It is initialized YES, then is @@ -178,6 +177,9 @@ typedef void (^HippyApplierVirtualBlock)(NSDictionary)domNode; +- (const std::weak_ptr &)domNode; + /** * Calculate property changes that need to be propagated to the view. * The applierBlocks set contains HippyApplierBlock functions that must be applied diff --git a/ios/sdk/component/view/HippyShadowView.mm b/ios/sdk/component/view/HippyShadowView.mm index 290a21141..1b7205e1c 100644 --- a/ios/sdk/component/view/HippyShadowView.mm +++ b/ios/sdk/component/view/HippyShadowView.mm @@ -32,7 +32,7 @@ CGRect getShadowViewRectFromDomNode(HippyShadowView *shadowView) { if (shadowView) { - std::shared_ptr node = shadowView.domNode; + std::shared_ptr node = shadowView.domNode.lock(); if (node) { const hippy::LayoutResult &layoutResult = node->GetLayoutResult(); return CGRectMake(layoutResult.left, layoutResult.top, layoutResult.width, layoutResult.height); @@ -66,6 +66,7 @@ typedef NS_ENUM(unsigned int, meta_prop_t) { float _paddingMetaProps[META_PROP_COUNT]; float _marginMetaProps[META_PROP_COUNT]; float _borderMetaProps[META_PROP_COUNT]; + std::weak_ptr _domNode; } @end @@ -427,6 +428,14 @@ typedef NS_ENUM(unsigned int, meta_prop_t) { return _textLifecycle != HippyUpdateLifecycleComputed; } +- (void)setDomNode:(std::weak_ptr)domNode { + _domNode = domNode; +} + +- (const std::weak_ptr &)domNode { + return _domNode; +} + - (void)setTextComputed { _textLifecycle = HippyUpdateLifecycleComputed; } @@ -550,7 +559,12 @@ typedef NS_ENUM(unsigned int, meta_prop_t) { //HIPPY_PADDING_PROPERTY(Right, RIGHT) // - (UIEdgeInsets)paddingAsInsets { - return UIEdgeInsetsFromLayoutResult(self.domNode->GetLayoutResult()); + UIEdgeInsets insets = UIEdgeInsetsZero; + auto domNode = self.domNode.lock(); + if (domNode) { + insets = UIEdgeInsetsFromLayoutResult(domNode->GetLayoutResult()); + } + return insets; } // //// Border diff --git a/ios/sdk/module/uimanager/NativeRenderManager.mm b/ios/sdk/module/uimanager/NativeRenderManager.mm index 3dffe8597..f5b703825 100644 --- a/ios/sdk/module/uimanager/NativeRenderManager.mm +++ b/ios/sdk/module/uimanager/NativeRenderManager.mm @@ -30,13 +30,6 @@ using LayoutResult = hippy::LayoutResult; using CallFunctionCallback = hippy::CallFunctionCallback; void NativeRenderManager::CreateRenderNode(std::vector> &&nodes) { - for (auto it = nodes.begin(); it != nodes.end(); it++) { - if (0 == it->get()->GetViewName().compare("Text")) { - std::shared_ptr layoutNode = std::static_pointer_cast(it->get()->GetLayoutNode()); - layoutNode->SetMeasureFunction(textMeasureFunc); - HPNodeSetContext(layoutNode->GetLayoutEngineNodeRef(), it->get()); - } - } [uiManager_ createRenderNodes:std::move(nodes)]; }