Skip to content

Commit 99de3b6

Browse files
committed
fix(android): lineBreak working with verticalAlignment
1 parent dc3d55d commit 99de3b6

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

packages/ui-canvas/platforms/android/java/com/akylas/canvas/StaticLayout.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,30 @@ public static android.text.StaticLayout.Builder createStaticLayoutBuilder(CharSe
3030
}
3131

3232
public static android.text.StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width,
33-
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
34-
33+
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int height) {
34+
android.text.StaticLayout staticLayout = null;
3535
if (Build.VERSION.SDK_INT >= 23) {
36-
return createStaticLayoutBuilder(source, paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth).build();
36+
staticLayout = createStaticLayoutBuilder(source, paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth).build();
3737
} else {
38-
return new android.text.StaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth);
38+
staticLayout = new android.text.StaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth);
3939
}
40+
return createEllipsizeStaticLayout(staticLayout, includepad, ellipsize, ellipsizedWidth, height);
41+
}
42+
public static android.text.StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width,
43+
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
44+
return createStaticLayout(source, paint, width, align, spacingmult, spacingadd, includepad,ellipsize, ellipsizedWidth, -1);
45+
}
46+
47+
public static android.text.StaticLayout createStaticLayout(CharSequence source, Paint paint, int width,
48+
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int height) {
49+
return createStaticLayout(source, new TextPaint(paint), width, align, spacingmult, spacingadd, includepad,ellipsize, ellipsizedWidth, height);
4050
}
41-
4251
public static android.text.StaticLayout createStaticLayout(CharSequence source, Paint paint, int width,
4352
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
44-
return createStaticLayout(source, new TextPaint(paint), width, align, spacingmult, spacingadd, includepad,ellipsize, ellipsizedWidth);
53+
return createStaticLayout(source, paint, width, align, spacingmult, spacingadd, includepad,ellipsize, ellipsizedWidth, -1);
4554
}
4655

47-
public static void draw(android.text.StaticLayout staticLayout, Canvas canvas, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxHeight) {
56+
public static android.text.StaticLayout createEllipsizeStaticLayout(android.text.StaticLayout staticLayout, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxHeight) {
4857
if (maxHeight != -1 && ellipsize != null) {
4958

5059
// Calculate the number of lines that fit within the available height
@@ -74,7 +83,7 @@ public static void draw(android.text.StaticLayout staticLayout, Canvas canvas, b
7483
ellipsize,
7584
ellipsizedWidth);
7685
builder.setMaxLines(maxLines);
77-
staticLayout = builder.build();
86+
return builder.build();
7887
} else {
7988
int truncationIndex = staticLayout.getLineEnd(maxLines - 1);
8089

@@ -96,7 +105,7 @@ public static void draw(android.text.StaticLayout staticLayout, Canvas canvas, b
96105
}
97106

98107
// Re-create the StaticLayout with the truncated text
99-
staticLayout = createStaticLayout(new SpannableString(truncatedText), staticLayout.getPaint(),
108+
return createStaticLayout(new SpannableString(truncatedText), staticLayout.getPaint(),
100109
staticLayout.getWidth(),
101110
staticLayout.getAlignment(),
102111
staticLayout.getSpacingMultiplier(),
@@ -108,6 +117,6 @@ public static void draw(android.text.StaticLayout staticLayout, Canvas canvas, b
108117
}
109118
}
110119
}
111-
staticLayout.draw(canvas);
120+
return staticLayout;
112121
}
113122
}

src/ui-canvas/index.android.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ function lineBreakToEllipsize(value) {
437437
}
438438

439439
export class StaticLayout extends ProxyClass<android.text.StaticLayout> {
440-
ellipsize: android.text.TextUtils.TruncateAt;
440+
// ellipsize: android.text.TextUtils.TruncateAt;
441441
static nonNativeMethods = ['draw'];
442442
constructor(
443443
text: any,
@@ -446,9 +446,10 @@ export class StaticLayout extends ProxyClass<android.text.StaticLayout> {
446446
align = LayoutAlignment.ALIGN_NORMAL,
447447
spacingmult = 1,
448448
spacingadd = 0,
449-
private includepad = true,
449+
includepad = true,
450450
ellipsize = null,
451-
private ellipsizedWidth = width
451+
ellipsizedWidth = width,
452+
height
452453
) {
453454
super();
454455
paint = (paint as any).getNative ? (paint as any).getNative() : paint;
@@ -457,8 +458,18 @@ export class StaticLayout extends ProxyClass<android.text.StaticLayout> {
457458
// in case it is a number or a boolean
458459
text = text + '';
459460
}
460-
this.ellipsize = lineBreakToEllipsize(ellipsize);
461-
this.mNative = com.akylas.canvas.StaticLayout.createStaticLayout(text, paint, width, align, spacingmult, spacingadd, includepad, this.ellipsize, ellipsizedWidth);
461+
this.mNative = com.akylas.canvas.StaticLayout.createStaticLayout(
462+
text,
463+
paint,
464+
width,
465+
align,
466+
spacingmult,
467+
spacingadd,
468+
includepad,
469+
lineBreakToEllipsize(ellipsize),
470+
ellipsizedWidth,
471+
Math.round(height)
472+
);
462473
return this;
463474
}
464475

@@ -479,7 +490,8 @@ export class StaticLayout extends ProxyClass<android.text.StaticLayout> {
479490
}
480491

481492
draw(canvas: Canvas, maxHeight = -1) {
482-
com.akylas.canvas.StaticLayout.draw(this.getNative(), canvas.getNative(), this.includepad, this.ellipsize, this.ellipsizedWidth, maxHeight);
493+
this.getNative().draw(canvas.getNative());
494+
// com.akylas.canvas.StaticLayout.draw(this.getNative(), canvas.getNative(), this.includepad, this.ellipsize, this.ellipsizedWidth, maxHeight);
483495
}
484496
}
485497
let Cap, Direction, DrawFilter, FillType, Join, Matrix, Op, PathEffect, Rect, RectF, Style, TileMode, FontMetrics, Align, LayoutAlignment;

src/ui-canvas/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class Paint {
9494
// export class StaticLayout {
9595
// }
9696
export class StaticLayout extends android.text.StaticLayout {
97-
constructor(text: any, paint: Paint, width: number, align?, spacingmult?, spacingadd?, includepad?, ellipsize?, ellipsizedWidth?);
97+
constructor(text: any, paint: Paint, width: number, align?, spacingmult?, spacingadd?, includepad?, ellipsize?, ellipsizedWidth?, height?);
9898
public draw(canvas: any, path?: any, paint?: any, param3?: number): void;
9999

100100
static getDesiredWidth(text: any, paint: any);

src/ui-canvas/typings/android.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ declare namespace com {
3838
spacingadd: number,
3939
includepad: boolean,
4040
ellipsize: globalAndroid.text.TextUtils.TruncateAt,
41-
ellipsizedWidth: number
41+
ellipsizedWidth: number,
42+
height: number
4243
): android.text.StaticLayout;
4344
static draw(
4445
staticLayout: globalAndroid.text.StaticLayout,

src/ui-canvaslabel/canvaslabel.android.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import { createNativeAttributedString, createSpannable, typefaceCache } from '@n
77
export class Span extends SpanBase {
88
_ssb: android.text.SpannableStringBuilder;
99

10-
@profile
1110
createNative(parentCanvas: CanvasLabelBase, parent?: Group, maxFontSize?: number) {
1211
this.mNative = this._ssb = createSpannable(this, parentCanvas, parent, maxFontSize);
1312
}
13+
// createStaticLayout(text, w, h, align, parent: CanvasLabel) {
14+
// this.mStaticlayout = super.createStaticLayout(text, w, h, align, parent);
15+
// return this.mStaticlayout;
16+
// }
1417
}
1518

1619
// const NSPan = com.nativescript.canvaslabel.Span;
1720
export class Group extends GroupBase {
1821
_ssb: android.text.SpannableStringBuilder;
1922

20-
@profile
2123
createNative(parentCanvas: CanvasLabelBase, parent?: Group, maxFontSize?: number) {
2224
if (!this.mSpans) {
2325
this.mNative = null;

src/ui-canvaslabel/canvaslabel.common.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ export abstract class Span extends Shape {
209209
parent.redraw();
210210
}
211211
}
212-
@profile
213-
createStaticLayout(text, w, align, parent: CanvasLabel) {
212+
createStaticLayout(text, w, h, align, parent: CanvasLabel) {
214213
const fontweight = this.fontWeight;
215214
const fontstyle = this.fontStyle || parent.style.fontStyle || parent.fontStyle;
216215
const fontFamily = this.fontFamily;
@@ -240,7 +239,7 @@ export abstract class Span extends Shape {
240239
if (color) {
241240
cachedPaint.color = color;
242241
}
243-
this.mStaticlayout = new StaticLayout(text, cachedPaint, w, align, 1, 0, true, lineBreak);
242+
this.mStaticlayout = new StaticLayout(text, cachedPaint, w, align, 1, 0, true, lineBreak, w, h);
244243
return this.mStaticlayout;
245244
}
246245

@@ -324,7 +323,7 @@ export abstract class Span extends Shape {
324323
}
325324
let staticlayout = this.mStaticlayout;
326325
if (!staticlayout) {
327-
staticlayout = this.createStaticLayout(text, Math.max(0, w), align, parent);
326+
staticlayout = this.createStaticLayout(text, Math.max(0, w), h, align, parent);
328327
}
329328
let _staticWidth;
330329
const getStaticWidth = () => {

0 commit comments

Comments
 (0)