/
SDAdaptiveTextView.java
76 lines (69 loc) · 2.96 KB
/
SDAdaptiveTextView.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
package com.siberiadante.androidutil.widget.textview;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created: SiberiaDante
* Email: 2654828081@qq.com
* GitHub: https://github.com/SiberiaDante
* Blog: http://www.cnblogs.com/shen-hua/
* CreateTime: 2018/3/1
* UpDateTime:
* Describe: 解决TextView的内容中包含中英文导致自动换行bug,会有性能影响,可以考虑使用文字转半角使用
*/
public class SDAdaptiveTextView extends TextView {
public SDAdaptiveTextView(Context context) {
super(context);
}
public SDAdaptiveTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SDAdaptiveTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
}
/**
* 设置TextView的文本内容,改方法不能再主线程中执行
*
* @param text
*/
public void setAdaptiveText(String text) {
this.setText(text);
this.setText(adaptiveText(this));
}
private String adaptiveText(final TextView textView) {
final String originalText = textView.getText().toString(); //原始文本
final Paint tvPaint = textView.getPaint();//获取TextView的Paint
final float tvWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight(); //TextView的可用宽度
//将原始文本按行拆分
String[] originalTextLines = originalText.replaceAll("\r", "").split("\n");
StringBuilder newTextBuilder = new StringBuilder();
for (String originalTextLine : originalTextLines) {
//文本内容小于TextView宽度,即不换行,不作处理
if (tvPaint.measureText(originalTextLine) <= tvWidth) {
newTextBuilder.append(originalTextLine);
} else {
//如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
float lineWidth = 0;
for (int i = 0; i != originalTextLine.length(); ++i) {
char charAt = originalTextLine.charAt(i);
lineWidth += tvPaint.measureText(String.valueOf(charAt));
if (lineWidth <= tvWidth) {
newTextBuilder.append(charAt);
} else {
//单行超过TextView可用宽度,换行
newTextBuilder.append("\n");
lineWidth = 0;
--i;//该代码作用是将本轮循环回滚,在新的一行重新循环判断该字符
}
}
}
}
return newTextBuilder.toString();
}
}