Skip to content

support max lines for labels #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ Attributes:
<td>Color for the labels</td>
<td>android:textColorSecondary defined in your project</td>
</tr>
<tr>
<td>stpi_labelMaxLines</td>
<td>Max number of lines for the labels. Ellipsized at the end if exceeded. Only above SDK 23.</td>
<td>infinite</td>
</tr>
</tbody></table>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
Expand Down Expand Up @@ -179,6 +181,11 @@
* <td>Color for the labels</td>
* <td>android:textColorSecondary defined in your project</td>
* </tr>
* <tr>
* <td>stpi_labelMaxLines</td>
* <td>Max number of lines for the labels. Ellipsized at the end if exceeded. Only above SDK 23.</td>
* <td>infinite</td>
* </tr>
* </tbody></table>
* <p>
* <p> Updated by Ionut Negru on 08/08/16 to add the stepClickListener feature.</p>
Expand Down Expand Up @@ -354,6 +361,7 @@ public class StepperIndicator extends View implements ViewPager.OnPageChangeList
private CharSequence[] labels;
private boolean showLabels;
private float labelMarginTop;
private int labelMaxLines;
private StaticLayout[] labelLayouts;
private float maxLabelHeight;

Expand Down Expand Up @@ -626,6 +634,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
float defaultLabelMarginTop = resources.getDimension(R.dimen.stpi_default_label_margin_top);
labelMarginTop = typedArray.getDimension(R.styleable.StepperIndicator_stpi_labelMarginTop, defaultLabelMarginTop);

labelMaxLines = typedArray.getInt(R.styleable.StepperIndicator_stpi_labelMaxLines, Integer.MAX_VALUE);

showLabels(typedArray.getBoolean(R.styleable.StepperIndicator_stpi_showLabels, false));
setLabels(typedArray.getTextArray(R.styleable.StepperIndicator_stpi_labels));

Expand Down Expand Up @@ -825,6 +835,17 @@ private void calculateMaxLabelHeight(final int measuredWidth) {

labelLayouts[i] = new StaticLayout(labels[i], labelPaint, gridWidth,
Layout.Alignment.ALIGN_NORMAL, 1, 0, false);

if (labelMaxLines < Integer.MAX_VALUE && labelLayouts[i].getLineCount() > labelMaxLines && Build.VERSION.SDK_INT >= 23) {
// recreate StaticLayout if it needs to be ellipsized
labelLayouts[i]= StaticLayout.Builder
.obtain(labels[i], 0, labels[i].length(), labelPaint, gridWidth)
.setMaxLines(labelMaxLines)
.setLineSpacing(0, 1)
.setIncludePad(false)
.setEllipsize(TextUtils.TruncateAt.END)
.build();
}
maxLabelHeight = Math.max(maxLabelHeight, labelLayouts[i].getLineCount() * labelSingleLineHeight);
}
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
<attr name="stpi_labelMarginTop" format="dimension"/>
<attr name="stpi_labelSize" format="dimension"/>
<attr name="stpi_labelColor" format="color"/>
<attr name="stpi_labelMaxLines" format="integer"/>
</declare-styleable>
</resources>