fix(ios): text layout measure function modify

This commit is contained in:
luomy
2021-12-21 17:52:11 +08:00
parent fcec753863
commit 4df7d11479
5 changed files with 82 additions and 31 deletions
+43 -19
View File
@@ -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<hippy::DomNode *>(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<hippy::DomNode *>(HPNodeGetContext(node));
std::shared_ptr<hippy::DomNode> 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 <Text> must have a width and height");
}
@@ -684,6 +691,23 @@ HIPPY_TEXT_PROPERTY(TextShadowColor, _textShadowColor, UIColor *);
return newProps;
}
- (void)setDomNode:(std::weak_ptr<hippy::DomNode>)domNode {
[super setDomNode:domNode];
std::shared_ptr<hippy::DomNode> 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_ptr<hippy::TaitankLayoutNode>layoutNode =
std::static_pointer_cast<hippy::TaitankLayoutNode>(node->GetLayoutNode());
layoutNode->SetMeasureFunction(measureFunc);
}
}
- (void)setText:(NSString *)text {
double version = UIDevice.currentDevice.systemVersion.doubleValue;
if (version >= 10.0 && version < 12.0) {
@@ -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<hippy::DomNode>)domNode {
[super setDomNode:domNode];
std::shared_ptr<hippy::DomNode> 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_ptr<hippy::TaitankLayoutNode>layoutNode =
std::static_pointer_cast<hippy::TaitankLayoutNode>(node->GetLayoutNode());
layoutNode->SetMeasureFunction(measureFunc);
}
}
/*
* text类型控件会响应用户输入交互,但是并不会更新shadowText中的props属性,
* 导致前端下发新的props属性与当前属性实际值不一致
+3 -1
View File
@@ -75,7 +75,6 @@ typedef void (^HippyApplierVirtualBlock)(NSDictionary<NSNumber *, HippyVirtualNo
@property (nonatomic, copy) NSString *visibility;
@property (nonatomic, assign) BOOL visibilityChanged;
@property (nonatomic, assign) BOOL hasNewLayout;
@property (nonatomic, assign) std::shared_ptr<hippy::DomNode> 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<NSNumber *, HippyVirtualNo
*/
@property (nonatomic, assign) OverflowType overflow;
- (void)setDomNode:(std::weak_ptr<hippy::DomNode>)domNode;
- (const std::weak_ptr<hippy::DomNode> &)domNode;
/**
* Calculate property changes that need to be propagated to the view.
* The applierBlocks set contains HippyApplierBlock functions that must be applied
+16 -2
View File
@@ -32,7 +32,7 @@
CGRect getShadowViewRectFromDomNode(HippyShadowView *shadowView) {
if (shadowView) {
std::shared_ptr<hippy::DomNode> node = shadowView.domNode;
std::shared_ptr<hippy::DomNode> 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<hippy::DomNode> _domNode;
}
@end
@@ -427,6 +428,14 @@ typedef NS_ENUM(unsigned int, meta_prop_t) {
return _textLifecycle != HippyUpdateLifecycleComputed;
}
- (void)setDomNode:(std::weak_ptr<hippy::DomNode>)domNode {
_domNode = domNode;
}
- (const std::weak_ptr<hippy::DomNode> &)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
@@ -30,13 +30,6 @@ using LayoutResult = hippy::LayoutResult;
using CallFunctionCallback = hippy::CallFunctionCallback;
void NativeRenderManager::CreateRenderNode(std::vector<std::shared_ptr<DomNode>> &&nodes) {
for (auto it = nodes.begin(); it != nodes.end(); it++) {
if (0 == it->get()->GetViewName().compare("Text")) {
std::shared_ptr<hippy::TaitankLayoutNode> layoutNode = std::static_pointer_cast<hippy::TaitankLayoutNode>(it->get()->GetLayoutNode());
layoutNode->SetMeasureFunction(textMeasureFunc);
HPNodeSetContext(layoutNode->GetLayoutEngineNodeRef(), it->get());
}
}
[uiManager_ createRenderNodes:std::move(nodes)];
}