diff --git a/README.md b/README.md index 0cb4111..17412b0 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,11 @@ Attributes: Color for the labels android:textColorSecondary defined in your project + + stpi_labelMaxLines + Max number of lines for the labels. Ellipsized at the end if exceeded. Only above SDK 23. + infinite + diff --git a/library/src/main/java/com/rakshakhegde/stepperindicator/StepperIndicator.java b/library/src/main/java/com/rakshakhegde/stepperindicator/StepperIndicator.java index bece5d9..47b9e89 100644 --- a/library/src/main/java/com/rakshakhegde/stepperindicator/StepperIndicator.java +++ b/library/src/main/java/com/rakshakhegde/stepperindicator/StepperIndicator.java @@ -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; @@ -179,6 +181,11 @@ * Color for the labels * android:textColorSecondary defined in your project * + * + * stpi_labelMaxLines + * Max number of lines for the labels. Ellipsized at the end if exceeded. Only above SDK 23. + * infinite + * * *

*

Updated by Ionut Negru on 08/08/16 to add the stepClickListener feature.

@@ -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; @@ -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)); @@ -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); } } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 77237e7..3ccff7e 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -34,5 +34,6 @@ +