1. 程式人生 > >一個TextView 多行文字省略

一個TextView 多行文字省略

前提:公司需求,在圖中右側的tip中,如果資料過長,就在尾部省略。返回的資料是 ("區域性多雲,不利於健康")

原本是一個TextView,雖然用兩個textview最容易實現,但是感覺太low了吧。

於是乎,學了這麼久的自定義View 終於派上用場了。

哈哈哈哈嗝。。。。

不說廢話,上程式碼!

CharSequence text = getText();
int width = getWidth();
float textSize = getTextSize();
float textScaleX = getTextScaleX();
int textCount = (int) ((width-getPaddingLeft()-getPaddingRight()) / (textSize+textScaleX));
String[] split = text.toString().split("\n");
LG.e("lmy", "text="+text+",width="+width+",size="+textSize+",textCount="+textCount+",split="+Arrays.toString(split));
StringBuffer buffer=new StringBuffer();
for (int i = 0; i < split.length; i++) {
    String s = split[i];
    if (s.length() > textCount&&textCount>0) {
        buffer.append(s.substring(0, textCount - 1));
        buffer.append("...");
    } else {
        buffer.append(s);
    }
    if (i < split.length - 1) {
        buffer.append("\n");
    }
}
String result = buffer.toString();

首先 ,前面幾步的獲取我就不多說了。

width-getPaddingLeft()-getPaddingRight()   總的寬度減去左右內邊距,沒意見吧。

int textCount = (int) ((width-getPaddingLeft()+getPaddingRight()) / (textSize+textScaleX));字型總寬度除以字型的大小加間距

getTextScaleX()這個方法是獲取自體間距的

String[] split = text.toString().split("\n"); 這裡因為是用\n換行,所以這裡在擷取一下 對每一行的自體數量做下判斷。

for (int i = 0; i < split.length; i++) {
    String s = split[i];
    if (s.length() > textCount&&textCount>0) {
        buffer.append(s.substring(0, textCount - 1));
        buffer.append("...");
    } else {
        buffer.append(s);
    }
    if (i < split.length - 1) {
        buffer.append("\n");
    }
}

這一部分就是迴圈判斷每一行自體的個數有沒有超過最大的限制 有的話就把最後一個字元刪除掉用...代替

下面新增\n的時候做了個判斷最後一行不會新增,保證字型垂直居中。

呼叫的話我是在onLayout裡呼叫的,在setText裡呼叫的話有可能寬度測試不準所以在onLayout裡呼叫

下面附上全部程式碼和效果圖

效果圖

 全部程式碼


public class CustomTextView extends TextView{
    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        setText(updateTip().toString());
    }

    private String updateTip(){
        CharSequence text = getText();
        int width = getWidth();
        float textSize = getTextSize();
        float textScaleX = getTextScaleX();
        int textCount = (int) ((width-getPaddingLeft()+getPaddingRight()) / (textSize+textScaleX));
        String[] split = text.toString().split("\n");
        LG.e("lmy", "text="+text+",width="+width+",size="+textSize+",textCount="+textCount+",split="+Arrays.toString(split));
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if (s.length() > textCount&&textCount>0) {
                buffer.append(s.substring(0, textCount - 1));
                buffer.append("...");
            } else {
                buffer.append(s);
            }
            if (i < split.length - 1) {
                buffer.append("\n");
            }
        }
        return buffer.toString();
    }
}