From 028ea243f5d2957f958a5c4cd42aa8e905b7dc1b Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 11 Dec 2017 13:51:22 +0800 Subject: [PATCH] add method getMoreLength() to calculate replace string length. --- .../java/com/lovejjfg/demo/MainActivity.kt | 4 +++- .../powertext/ExpandableTextView.java | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/lovejjfg/demo/MainActivity.kt b/app/src/main/java/com/lovejjfg/demo/MainActivity.kt index b46abc6..310eee5 100644 --- a/app/src/main/java/com/lovejjfg/demo/MainActivity.kt +++ b/app/src/main/java/com/lovejjfg/demo/MainActivity.kt @@ -28,7 +28,9 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val originalText = "奶茶妹妹章泽天成功入选2017年达沃斯全球杰出青年。全球杰出青年社区是世界经济论坛(达沃斯论坛)成立的多方利益相关者社区之一,致力于网罗全球最优秀青年才俊,改善世界状况。全球杰出青年的评选标准包括其卓越的职业成就和潜在领导力以及致力于推动变革的承诺。" +// val originalText = "Sister zhang zetian has been selected as the world's outstanding young man in davos in 2017. The world's outstanding youth community is one of the 1086 multi-stakeholder communities established in the world economy BBS (BBS), which aims to recruit the best young talents in the world and improve the situation of the world. The selection criteria for the world's outstanding young people include its outstanding professional achievements and potential leadership and commitment to promoting change." + val originalText = "奶茶妹妹章泽天成功入选2017年达沃斯全球杰出青年。全球杰出青年社区是世界经济论坛(达沃斯论坛)成立的多方利益相关者社区之10086一,致力于网罗全球最优秀青年才俊,改善世界状况。全球杰出青年的评选标准包括其卓越的职业成就和潜在领导力以及致力于推动变革的承诺。" +// val originalText = "奶茶妹妹章泽天成功入选2017年达沃斯全球杰出青年。全球杰出青年社区是世界经济论坛(达沃斯论坛)成立的多方利益相关者社区之一,致力于网罗全球最优秀青年才俊,改善世界状况。全球杰出青年的评选标准包括其卓越的职业成就和潜在领导力以及致力于推动变革的承诺。" text.setLabelText("奶茶妹妹") text.setOriginalText(originalText) diff --git a/lib/src/main/java/com/lovejjfg/powertext/ExpandableTextView.java b/lib/src/main/java/com/lovejjfg/powertext/ExpandableTextView.java index d19aa01..2f14079 100644 --- a/lib/src/main/java/com/lovejjfg/powertext/ExpandableTextView.java +++ b/lib/src/main/java/com/lovejjfg/powertext/ExpandableTextView.java @@ -122,15 +122,17 @@ public boolean onPreDraw() { return true; } int lineCount = layout.getLineCount(); - System.out.println("lineCount:" + lineCount); if (lineCount > mDefaultLineCount) { String substring; + int moreLength = 0; + String mText; if (!TextUtils.isEmpty(mLabelText)) { - String mText = "{" + mLabelText + "}" + mOriginalText; - substring = mText.substring(0, layout.getLineEnd(mDefaultLineCount - 1) - mMoreHint.length() - 1); + mText = "{" + mLabelText + "}" + mOriginalText; } else { - substring = mOriginalText.toString().substring(0, layout.getLineEnd(mDefaultLineCount - 1) - mMoreHint.length() - 1); + mText = mOriginalText.toString(); } + moreLength = getMoreLength(layout, moreLength, mText); + substring = mText.substring(0, layout.getLineEnd(mDefaultLineCount - 1) - moreLength); SpannableStringBuilder mOriginalBuilder = new SpannableStringBuilder(String.format("%s...%s", substring, mMoreHint)); mOriginalBuilder.setSpan(new ClickableSpan() { @Override @@ -162,6 +164,18 @@ public void updateDrawState(TextPaint ds) { }); } + private int getMoreLength(Layout layout, int moreLength, String mText) { + int lastLine = mDefaultLineCount - 1; + moreLength++; + String newText = mText.substring(layout.getLineStart(lastLine), layout.getLineEnd(lastLine) - moreLength); + + while (getPaint().measureText(newText + "..." + mMoreHint) > layout.getWidth()) { + moreLength++; + newText = mText.substring(layout.getLineStart(lastLine), layout.getLineEnd(lastLine) - moreLength); + } + return moreLength; + } + @Override public void onRestoreInstanceState(Parcelable state) { if (!(state instanceof SavedState)) {