/
TextWrapper.java
101 lines (85 loc) · 2.61 KB
/
TextWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.qliktrialreactnativestraighttable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;
import android.widget.TextView;
public class TextWrapper {
final TableView tableView;
final TextView textView;
DataColumn column;
TextPaint measureTextPaint = new TextPaint();
int lineCount = 1;
int wordCount = 0;
int additionalPadding = 0;
TextWrapper(DataColumn column, TableView tableView, TextView textView) {
this.column = column;
this.tableView = tableView;
this.textView = textView;
measureTextPaint.setTextSize(textView.getTextSize());
measureTextPaint.setAntiAlias(true);
}
TextWrapper(TableView tableView, TextView textView) {
this.tableView = tableView;
this.textView = textView;
measureTextPaint.setTextSize(textView.getTextSize());
measureTextPaint.setAntiAlias(true);
}
public void testTextWrap() {
measureLineCount();
}
public void testTextWrap(DataColumn dataColumn) {
this.column = dataColumn;
measureLineCount();
}
public void testOnlyTextWrap() {
measureLineCountNoUpdate();
}
void measureLineCount() {
if (wordCount > 1) {
int lines = calculateLineCount();
if (lines != lineCount) {
lineCount = Math.min(lines, wordCount);
tableView.updateHeaderViewLineCount();
}
}
}
void measureLineCountNoUpdate() {
if (wordCount > 1) {
int lines = calculateLineCount();
if (lines != lineCount) {
lineCount = Math.min(lines, wordCount);
}
}
}
protected int calculateLineCount() {
int width = Math.max(column.width - textView.getPaddingRight() - textView.getPaddingLeft() - additionalPadding, 0);
measureTextPaint.setTypeface(textView.getTypeface());
StaticLayout.Builder builder = StaticLayout.Builder.obtain(textView.getText(), 0, textView.getText().length(), measureTextPaint, width);
builder.setIncludePad(true);
builder.setMaxLines(wordCount);
builder.setAlignment(Layout.Alignment.ALIGN_NORMAL);
builder.setLineSpacing(1, 1);
StaticLayout layout = builder.build();
int lines = layout.getLineCount();
return lines;
}
public int getMeasuredLinedCount() {
return lineCount;
}
public int setMaxLines(int maxLines, DataColumn column) {
if(column.isDim) {
countWords(textView.getText().toString());
maxLines = wordCount > 1 ? maxLines : 1;
} else {
maxLines = 1;
wordCount = 1;
}
return maxLines;
}
public void countWords(String text) {
if(text != null) {
wordCount = text.split("\\s+").length;
}
}
}