List of usage examples for android.text StaticLayout getLineEnd
public final int getLineEnd(int line)
From source file:android.support.v7.widget.AppCompatTextViewAutoSizeHelper.java
private boolean suggestedSizeFitsInSpace(int suggestedSizeInPx, RectF availableSpace) { CharSequence text = mTextView.getText(); TransformationMethod transformationMethod = mTextView.getTransformationMethod(); if (transformationMethod != null) { CharSequence transformedText = transformationMethod.getTransformation(text, mTextView); if (transformedText != null) { text = transformedText;/* ww w . j a va2s .c o m*/ } } final int maxLines = Build.VERSION.SDK_INT >= 16 ? mTextView.getMaxLines() : -1; if (mTempTextPaint == null) { mTempTextPaint = new TextPaint(); } else { mTempTextPaint.reset(); } mTempTextPaint.set(mTextView.getPaint()); mTempTextPaint.setTextSize(suggestedSizeInPx); // Needs reflection call due to being private. Layout.Alignment alignment = invokeAndReturnWithDefault(mTextView, "getLayoutAlignment", Layout.Alignment.ALIGN_NORMAL); final StaticLayout layout = Build.VERSION.SDK_INT >= 23 ? createStaticLayoutForMeasuring(text, alignment, Math.round(availableSpace.right), maxLines) : createStaticLayoutForMeasuringPre23(text, alignment, Math.round(availableSpace.right)); // Lines overflow. if (maxLines != -1 && (layout.getLineCount() > maxLines || (layout.getLineEnd(layout.getLineCount() - 1)) != text.length())) { return false; } // Height overflow. if (layout.getHeight() > availableSpace.bottom) { return false; } return true; }
From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java
private void calculateUsingTextSize(final float textSize) { if (mText == null) return;/*from w ww . java 2s . com*/ 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 } }