FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(ios): fix label text wrapping inside flexbox layout · NativeScript/NativeScript@31fe00d · GitHub

Commit 31fe00d

Browse files
committed
fix(ios): fix label text wrapping inside flexbox layout
1 parent 7edf561 commit 31fe00d

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎tns-core-modules/ui/label/label.ios.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export class Label extends TextBase implements LabelDefinition {
6868
this._fixedSize = (widthMode === layout.EXACTLY ? FixedSize.WIDTH : FixedSize.NONE)
6969
| (heightMode === layout.EXACTLY ? FixedSize.HEIGHT : FixedSize.NONE);
7070

71-
const nativeSize = layout.measureNativeView(nativeView, width, widthMode, height, heightMode);
71+
// NOTE: utils.measureNativeView(...) relies on UIView.sizeThatFits(...) that
72+
// seems to have various issues when laying out UILabel instances.
73+
// We use custom measure logic here that relies on overriden
74+
// UILabel.textRectForBounds:limitedToNumberOfLines: in TNSLabel widget.
75+
const nativeSize = this._measureNativeView(width, widthMode, height, heightMode);
7276
let labelWidth = nativeSize.width;
7377

7478
if (this.textWrap && widthMode === layout.AT_MOST) {
@@ -85,6 +89,22 @@ export class Label extends TextBase implements LabelDefinition {
8589
}
8690
}
8791

92+
private _measureNativeView(width: number, widthMode: number, height: number, heightMode: number): { width: number, height: number } {
93+
const view = <UILabel>this.nativeViewProtected;
94+
95+
const nativeSize = view.textRectForBoundsLimitedToNumberOfLines(
96+
CGRectMake(
97+
0,
98+
0,
99+
widthMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : layout.toDeviceIndependentPixels(width),
100+
heightMode === 0 /* layout.UNSPECIFIED */ ? Number.POSITIVE_INFINITY : layout.toDeviceIndependentPixels(height)
101+
), 0).size;
102+
103+
nativeSize.width = layout.round(layout.toDevicePixels(nativeSize.width));
104+
nativeSize.height = layout.round(layout.toDevicePixels(nativeSize.height));
105+
return nativeSize;
106+
}
107+
88108
[whiteSpaceProperty.setNative](value: WhiteSpace) {
89109
const nativeView = this.nativeViewProtected;
90110
switch (value) {

‎tns-core-modules/ui/layouts/flexbox-layout/flexbox-layout.ios.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,25 @@ export class FlexboxLayout extends FlexboxLayoutBase {
595595
} else {
596596
accumulatedRoundError = rawCalculatedWidth - roundedCalculatedWidth;
597597
}
598-
child.measure(makeMeasureSpec(roundedCalculatedWidth, EXACTLY), makeMeasureSpec(child.getMeasuredHeight(), EXACTLY));
598+
599+
const childWidthMeasureSpec = makeMeasureSpec(roundedCalculatedWidth, EXACTLY);
600+
601+
// NOTE: for controls that support internal content wrapping (e.g. UILabel) reducing the width
602+
// might result in increased height e.g. text that could be shown on one line for larger
603+
// width needs to be wrapped in two when width is reduced.
604+
// As a result we cannot unconditionally measure with EXACTLY the current measured height
605+
const childHeightMeasureSpec = FlexboxLayout.getChildMeasureSpec(this._currentHeightMeasureSpec,
606+
lp.effectivePaddingTop + lp.effectivePaddingBottom + lp.effectiveMarginTop
607+
+ lp.effectiveMarginBottom, lp.effectiveHeight < 0 ? WRAP_CONTENT : lp.effectiveHeight);
608+
609+
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
599610
child.effectiveMinWidth = minWidth;
611+
612+
// make sure crossSize is up-to-date as child calculated height might have increased
613+
flexLine._crossSize = Math.max(
614+
flexLine._crossSize,
615+
child.getMeasuredHeight() + lp.effectiveMarginTop + lp.effectiveMarginBottom
616+
);
600617
}
601618
flexLine._mainSize += child.getMeasuredWidth() + lp.effectiveMarginLeft + lp.effectiveMarginRight;
602619
} else {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL