From caacdf0796c3487f1fc86c81362bf54e00886ae9 Mon Sep 17 00:00:00 2001 From: iPel Date: Fri, 6 Jan 2023 17:14:27 +0800 Subject: [PATCH] fix(android): text with negative letterSpacing wraps at wrong position --- .../tencent/mtt/hippy/dom/node/TextNode.java | 28 +++++++++---------- docs/en-us/style/appearance.md | 2 ++ docs/style/appearance.md | 2 ++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/TextNode.java b/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/TextNode.java index ee777f8ab0e..5c5e85553e2 100644 --- a/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/TextNode.java +++ b/android/sdk/src/main/java/com/tencent/mtt/hippy/dom/node/TextNode.java @@ -749,24 +749,24 @@ protected Layout createLayout(float width, FlexMeasureMode widthMode) { } catch (Throwable e) { LogUtils.d("TextNode", "createLayout: " + e.getMessage()); } - float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN; boolean unconstrainedWidth = widthMode == FlexMeasureMode.UNDEFINED || width < 0; - if (boring == null && (unconstrainedWidth || (!FlexConstants.isUndefined(desiredWidth) - && desiredWidth <= width))) { - layout = buildStaticLayout(text, textPaint, (int)Math.ceil(desiredWidth)); - } else if (boring != null && (unconstrainedWidth || boring.width <= width)) { + if (boring != null && (unconstrainedWidth || boring.width <= width)) { layout = BoringLayout.make(text, textPaint, boring.width, mTextAlign, getLineSpacingMultiplier(), mLineSpacingExtra, boring, true); } else { - layout = buildStaticLayout(text, textPaint, (int)Math.ceil(width)); - } - if (mNumberOfLines != UNSET && mNumberOfLines > 0) { - if (layout.getLineCount() > mNumberOfLines) { - int lastLineStart = layout.getLineStart(mNumberOfLines - 1); - int lastLineEnd = layout.getLineEnd(mNumberOfLines - 1); - if (lastLineStart < lastLineEnd) { - int measureWidth = (int)Math.ceil(unconstrainedWidth ? desiredWidth : width); - layout = truncateLayoutWithNumberOfLine(layout, measureWidth, mNumberOfLines); + float desiredWidth = Layout.getDesiredWidth(text, textPaint); + if (!unconstrainedWidth && (widthMode == FlexMeasureMode.EXACTLY || desiredWidth > width)) { + desiredWidth = width; + } + layout = buildStaticLayout(text, textPaint, (int) Math.ceil(desiredWidth)); + if (mNumberOfLines != UNSET && mNumberOfLines > 0) { + if (layout.getLineCount() > mNumberOfLines) { + int lastLineStart = layout.getLineStart(mNumberOfLines - 1); + int lastLineEnd = layout.getLineEnd(mNumberOfLines - 1); + if (lastLineStart < lastLineEnd) { + int measureWidth = (int)Math.ceil(unconstrainedWidth ? desiredWidth : width); + layout = truncateLayoutWithNumberOfLine(layout, measureWidth, mNumberOfLines); + } } } } diff --git a/docs/en-us/style/appearance.md b/docs/en-us/style/appearance.md index 11a6e18bc50..b900205f18a 100644 --- a/docs/en-us/style/appearance.md +++ b/docs/en-us/style/appearance.md @@ -182,6 +182,8 @@ The horizontal spacing behavior between text characters !> hippy-vue should use @hippy/vue-css-loader `2.14.1` or above version +!> On some Android devices, setting a negative value for letterSpacing has compatibility issues, which may cause line breaks at unexpected positions + | Type | Required | Supported Platforms | ------ | -------- | --- | | number | No | Android、iOS diff --git a/docs/style/appearance.md b/docs/style/appearance.md index ed629e537cd..1c3d339b15f 100644 --- a/docs/style/appearance.md +++ b/docs/style/appearance.md @@ -182,6 +182,8 @@ !> hippy-vue 需采用 @hippy/vue-css-loader `2.14.1` 以上版本 +!> 在部分 Android 设备上,letterSpacing 设置负值有兼容性问题,可能会导致在非预期的位置换行 + | 类型 | 必需 | 支持平台 | ------ | -------- | --- | | number | 否 | Android、iOS