alibaba / GaiaX

动态模板引擎是一套轻量化、跨平台、高性能的纯原生移动端卡片渲染动态化解决方案
https://youku-gaiax.github.io/
Apache License 2.0
1.2k stars 142 forks source link

fix: sizeWithTemplateItem:measureSize: return abnormal size #451

Closed JohnTsaii closed 10 months ago

JohnTsaii commented 10 months ago

https://github.com/alibaba/GaiaX/issues/445

CLAassistant commented 10 months ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

:white_check_mark: jingcheng1988
:x: JohnTsaii
You have signed the CLA already but the status is still pending? Let us recheck it.

JohnTsaii commented 10 months ago

@jingcheng1988 这个pr有问题吗?你们是否也遇到了size计算不对的问题

jingcheng1988 commented 10 months ago

@jingcheng1988 这个pr有问题吗?你们是否也遇到了size计算不对的问题

我这边review一下开源的代码,在main分支上提供轻量化的处理方案,你可以验证一下是否修复了你的问题

JohnTsaii commented 10 months ago

demo.zip data: { "flowerDTO": { "title": "送您1个小礼物", "content": "感谢xxxxxxxxxxxx,很热情,主动帮我111111~", "time": "08-11 16:40", "customerNo": "xxxxxx", "orderNo": "111111111", "url": "查看全部" } } 并未修复这个问题,上面是我们的demo 你可以看一下 @jingcheng1988

jingcheng1988 commented 10 months ago

demo.zip data: { "flowerDTO": { "title": "送您1个小礼物", "content": "感谢xxxxxxxxxxxx,很热情,主动帮我111111~", "time": "08-11 16:40", "customerNo": "xxxxxx", "orderNo": "111111111", "url": "查看全部" } } 并未修复这个问题,上面是我们的demo 你可以看一下 @jingcheng1988

根据你提供的模板和数据,我进行本地模拟,得出的结果如下: 视图创建:<GXRootView: 0x11b70a8f0; frame = (10 140; 370 169); clipsToBounds = YES; backgroundColor = UIExtendedSRGBColorSpace 0.247059 0.584314 0.631373 1; layer = <CALayer: 0x2806d50a0>> size计算:NSSize: {370, 169}

创建的视图和实际计算出来的尺寸是一致的

jingcheng1988 commented 10 months ago

我这边复现了一下,发现是一个临界case,最终的原因是由于 paraStyle.lineBreakMode = NSLineBreakByCharWrapping; 计算和creatview的最终是不一致导致的

// 生成AttributedString NSMutableAttributedString *attrStr = nil; if (attributeText) { attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:attributeText]; } else { attrStr = [[NSMutableAttributedString alloc] initWithString:text]; }

//增加段落 & font属性
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
[attrStr addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, text.length)];