List of usage examples for android.text StaticLayout getLineStart
@Override public int getLineStart(int line)
From source file:org.wikipedia.page.shareafact.SnippetImage.java
/** * If the title or text is too long we first reduce the font size. * If that is not enough it gets ellipsized. */// w ww . java 2 s. c o m private static StaticLayout optimizeTextSize(TextLayoutParams params, int maxHeight, int maxLines, float maxFontSize, float minFontSize) { final float threshold1 = 60.0f; final float threshold2 = 40.0f; final float extraStep1 = 3.0f; final float extraStep2 = 1.0f; boolean fits = false; StaticLayout textLayout = null; // Try decreasing font size first for (float fontSize = maxFontSize; fontSize >= minFontSize; fontSize -= 1.0f) { params.textPaint.setTextSize(fontSize); textLayout = buildLayout(params); if (textLayout.getHeight() <= maxHeight) { fits = true; break; } // make it go faster at the beginning... if (fontSize > threshold1) { fontSize -= extraStep1; } else if (fontSize > threshold2) { fontSize -= extraStep2; } } // Then do own ellipsize: cut text off after last fitting space and add "..." // Didn't want to cut off randomly in the middle of a line or word. if (!fits) { final String textStr = params.text.toString(); final int ellipsisLength = 3; final int ellipsisStart = textLayout != null ? textLayout.getLineStart(maxLines) - ellipsisLength : textStr.length(); final int end = textStr.lastIndexOf(' ', ellipsisStart) + 1; if (end > 0) { textLayout = buildLayout(new TextLayoutParams(params, textStr.substring(0, end) + "...")); if (textLayout.getLineCount() <= maxLines) { fits = true; } } } // last resort: use TextUtils.ellipsize() if (!fits) { final float textRatio = .87f; final float maxWidth = textRatio * maxLines * params.lineWidth; textLayout = buildLayout(new TextLayoutParams(params, TextUtils.ellipsize(params.text, params.textPaint, maxWidth, TextUtils.TruncateAt.END))); } return textLayout; }
From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java
private void calculateUsingTextSize(final float textSize) { if (mText == null) return;/*from w w w. j a va 2s .c om*/ final float availableWidth; final float newTextSize; boolean updateDrawText = false; // BEGIN MODIFICATION: Add maxLines variable int maxLines; // END MODIFICATION if (isClose(textSize, mCollapsedTextSize)) { availableWidth = mCollapsedBounds.width(); newTextSize = mCollapsedTextSize; mScale = 1f; if (mCurrentTypeface != mCollapsedTypeface) { mCurrentTypeface = mCollapsedTypeface; updateDrawText = true; } // BEGIN MODIFICATION: Set maxLines variable maxLines = 1; // END MODIFICATION } else { availableWidth = mExpandedBounds.width(); newTextSize = mExpandedTextSize; if (mCurrentTypeface != mExpandedTypeface) { mCurrentTypeface = mExpandedTypeface; updateDrawText = true; } if (isClose(textSize, mExpandedTextSize)) { // If we're close to the expanded text size, snap to it and use a scale of 1 mScale = 1f; } else { // Else, we'll scale down from the expanded text size mScale = textSize / mExpandedTextSize; } // BEGIN MODIFICATION: Set maxLines variable maxLines = this.maxLines; // END MODIFICATION } if (availableWidth > 0) { updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText; mCurrentTextSize = newTextSize; mBoundsChanged = false; } if (mTextToDraw == null || updateDrawText) { mTextPaint.setTextSize(mCurrentTextSize); mTextPaint.setTypeface(mCurrentTypeface); // BEGIN MODIFICATION: Text layout creation and text truncation StaticLayout layout = new StaticLayout(mText, mTextPaint, (int) availableWidth, Layout.Alignment.ALIGN_NORMAL, 1, 0, false); CharSequence truncatedText; if (layout.getLineCount() > maxLines) { int lastLine = maxLines - 1; CharSequence textBefore = lastLine > 0 ? mText.subSequence(0, layout.getLineEnd(lastLine - 1)) : ""; CharSequence lineText = mText.subSequence(layout.getLineStart(lastLine), layout.getLineEnd(lastLine)); // if last char in line is space, move it behind the ellipsis CharSequence lineEnd = ""; if (lineText.charAt(lineText.length() - 1) == ' ') { lineEnd = lineText.subSequence(lineText.length() - 1, lineText.length()); lineText = lineText.subSequence(0, lineText.length() - 1); } // insert ellipsis character lineText = TextUtils.concat(lineText, "\u2026", lineEnd); // if the text is too long, truncate it CharSequence truncatedLineText = TextUtils.ellipsize(lineText, mTextPaint, availableWidth, TextUtils.TruncateAt.END); truncatedText = TextUtils.concat(textBefore, truncatedLineText); } else { truncatedText = mText; } if (!TextUtils.equals(truncatedText, mTextToDraw)) { mTextToDraw = truncatedText; mIsRtl = calculateIsRtl(mTextToDraw); } final Layout.Alignment alignment; // Don't rectify gravity for RTL languages, Layout.Alignment does it already. switch (mExpandedTextGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) { case Gravity.CENTER_HORIZONTAL: alignment = Layout.Alignment.ALIGN_CENTER; break; case Gravity.RIGHT: case Gravity.END: alignment = Layout.Alignment.ALIGN_OPPOSITE; break; case Gravity.LEFT: case Gravity.START: default: alignment = Layout.Alignment.ALIGN_NORMAL; break; } mTextLayout = new StaticLayout(mTextToDraw, mTextPaint, (int) availableWidth, alignment, 1, 0, false); // END MODIFICATION } }