|
1 | 1 | package com.akylas.canvas;
|
2 | 2 |
|
3 | 3 | import android.os.Build;
|
4 |
| -import android.graphics.Typeface; |
| 4 | +import android.graphics.Canvas; |
5 | 5 | import android.graphics.Paint;
|
6 |
| -import android.text.TextPaint; |
| 6 | +import android.graphics.Typeface; |
7 | 7 | import android.text.Layout;
|
| 8 | +import android.text.SpannableString; |
| 9 | +import android.text.SpannableStringBuilder; |
| 10 | +import android.text.TextPaint; |
| 11 | +import android.text.TextUtils; |
8 | 12 | import java.lang.CharSequence;
|
9 | 13 |
|
10 | 14 | public class StaticLayout {
|
11 | 15 |
|
| 16 | + public static android.text.StaticLayout.Builder createStaticLayoutBuilder(CharSequence source, TextPaint paint, int width, |
| 17 | + Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 18 | + android.text.StaticLayout.Builder builder = android.text.StaticLayout.Builder |
| 19 | + .obtain(source, 0, source.length(), paint, width) |
| 20 | + .setBreakStrategy(android.text.Layout.BREAK_STRATEGY_SIMPLE) |
| 21 | + .setAlignment(align) |
| 22 | + .setLineSpacing(spacingadd, spacingmult) |
| 23 | + .setIncludePad(includepad) |
| 24 | + .setEllipsizedWidth(ellipsizedWidth) |
| 25 | + .setEllipsize(ellipsize); |
| 26 | + if (Build.VERSION.SDK_INT >= 26) { |
| 27 | + builder = builder.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_NONE); |
| 28 | + } |
| 29 | + return builder; |
| 30 | + } |
| 31 | + |
12 | 32 | public static android.text.StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width,
|
13 |
| - Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad) { |
14 |
| - |
15 |
| - if (Build.VERSION.SDK_INT >= 24) { |
16 |
| - android.text.StaticLayout.Builder builder = android.text.StaticLayout.Builder |
17 |
| - .obtain(source, 0, source.length(), paint, width) |
18 |
| - .setBreakStrategy(android.text.Layout.BREAK_STRATEGY_SIMPLE).setAlignment(align) |
19 |
| - .setLineSpacing(spacingadd, spacingmult).setIncludePad(includepad); |
20 |
| - if (Build.VERSION.SDK_INT >= 26) { |
21 |
| - builder = builder.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_NONE); |
22 |
| - } |
23 |
| - return builder.build(); |
| 33 | + Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { |
| 34 | + |
| 35 | + if (Build.VERSION.SDK_INT >= 23) { |
| 36 | + return createStaticLayoutBuilder(source, paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth).build(); |
24 | 37 | } else {
|
25 |
| - return new android.text.StaticLayout(source, paint, width, align, spacingmult, spacingadd, includepad); |
| 38 | + return new android.text.StaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth); |
26 | 39 | }
|
27 | 40 | }
|
28 | 41 |
|
29 | 42 | public static android.text.StaticLayout createStaticLayout(CharSequence source, Paint paint, int width,
|
30 |
| - Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad) { |
31 |
| - return createStaticLayout(source, new TextPaint(paint), width, align, spacingmult, spacingadd, includepad); |
| 43 | + 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); |
| 45 | + } |
| 46 | + |
| 47 | + public static void draw(android.text.StaticLayout staticLayout, Canvas canvas, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxHeight) { |
| 48 | + if (maxHeight != -1 && ellipsize != null) { |
| 49 | + |
| 50 | + // Calculate the number of lines that fit within the available height |
| 51 | + int lineCount = staticLayout.getLineCount(); |
| 52 | + int maxLines = lineCount; |
| 53 | + |
| 54 | + int i = 0; |
| 55 | + float lineHeight = staticLayout.getLineBottom(i) - staticLayout.getLineTop(i); |
| 56 | + float totalHeight = lineHeight * lineCount; |
| 57 | + |
| 58 | + while (totalHeight > maxHeight && maxLines > 0) { |
| 59 | + maxLines--; |
| 60 | + i++; |
| 61 | + lineHeight = staticLayout.getLineBottom(i) - staticLayout.getLineTop(i); |
| 62 | + totalHeight = lineHeight * maxLines; |
| 63 | + } |
| 64 | + // Check if truncation is needed |
| 65 | + if (maxLines < lineCount) { |
| 66 | + if (Build.VERSION.SDK_INT >= 23 && ellipsize == TextUtils.TruncateAt.END) { |
| 67 | + android.text.StaticLayout.Builder builder = createStaticLayoutBuilder(staticLayout.getText(), |
| 68 | + staticLayout.getPaint(), |
| 69 | + staticLayout.getWidth(), |
| 70 | + staticLayout.getAlignment(), |
| 71 | + staticLayout.getSpacingMultiplier(), |
| 72 | + staticLayout.getSpacingAdd(), |
| 73 | + includepad, |
| 74 | + ellipsize, |
| 75 | + ellipsizedWidth); |
| 76 | + builder.setMaxLines(maxLines); |
| 77 | + staticLayout = builder.build(); |
| 78 | + } else { |
| 79 | + int truncationIndex = staticLayout.getLineEnd(maxLines - 1); |
| 80 | + |
| 81 | + // Truncate the text by replacing the characters after the truncation index with ellipsis |
| 82 | + SpannableStringBuilder truncatedText = null; |
| 83 | + if (ellipsize == TextUtils.TruncateAt.END || ellipsize == TextUtils.TruncateAt.MARQUEE) { |
| 84 | + truncatedText = new SpannableStringBuilder(staticLayout.getText().subSequence(0, truncationIndex)); |
| 85 | + truncatedText.append("…"); |
| 86 | + |
| 87 | + } else if (ellipsize == TextUtils.TruncateAt.START) { |
| 88 | + truncatedText = new SpannableStringBuilder("…"); |
| 89 | + truncatedText.append(staticLayout.getText().subSequence(0, truncationIndex)); |
| 90 | + |
| 91 | + } else { |
| 92 | + int split = truncationIndex/2; |
| 93 | + truncatedText = new SpannableStringBuilder(staticLayout.getText().subSequence(0, split)); |
| 94 | + truncatedText.append("…"); |
| 95 | + truncatedText.append(staticLayout.getText().subSequence(split, truncationIndex)); |
| 96 | + } |
| 97 | + |
| 98 | + // Re-create the StaticLayout with the truncated text |
| 99 | + staticLayout = createStaticLayout(new SpannableString(truncatedText), staticLayout.getPaint(), |
| 100 | + staticLayout.getWidth(), |
| 101 | + staticLayout.getAlignment(), |
| 102 | + staticLayout.getSpacingMultiplier(), |
| 103 | + staticLayout.getSpacingAdd(), |
| 104 | + includepad, |
| 105 | + ellipsize, |
| 106 | + ellipsizedWidth); |
| 107 | + |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + staticLayout.draw(canvas); |
32 | 112 | }
|
33 | 113 | }
|
0 commit comments