Skip to content

Commit

Permalink
Merge pull request #119 from ShawnLin013/feature/Support-Text-Styles
Browse files Browse the repository at this point in the history
Feature/support text styles
  • Loading branch information
ShawnLin013 authored Nov 17, 2018
2 parents 3621ba1 + 2ad3f66 commit 1b1966b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ It's based on [android.widget.NumberPicker](https://android.googlesource.com/pla

## Features

- Customizable fonts(color, size, typeface)
- Customizable fonts(color, size, strikethrough, underline, typeface)
- Customizable dividers(color, distance, thickness)
- Horizontal and Vertical mode are both supported
- Ascending and Descending order are both supported
Expand Down Expand Up @@ -119,7 +119,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
### Attributes

|attribute name|attribute description|
|:-:|:-:|
|:---:|:---:|
|np_width|The width of this widget.|
|np_height|The height of this widget.|
|np_dividerColor|The color of the selection divider.|
Expand All @@ -133,10 +133,16 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_order|The order of this widget. Default is ascending.|
|np_orientation|The orientation of this widget. Default is vertical.|
|np_scrollerEnabled|Flag whether the scroller should enabled.|
|np_selectedTextAlign|The text align of the selected number. Default is center.|
|np_selectedTextColor|The text color of the selected number.|
|np_selectedTextSize|The text size of the selected number.|
|np_selectedTextStrikeThru|Flag whether the selected text should strikethroughed.|
|np_selectedTextUnderline|Flag whether the selected text should underlined.|
|np_textAlign|The text align of the numbers. Default is center.|
|np_textColor|The text color of the numbers.|
|np_textSize|The text size of the numbers.|
|np_textStrikeThru|Flag whether the text should strikethroughed.|
|np_textUnderline|Flag whether the text should underlined.|
|np_typeface|The typeface of the numbers.|
|np_value|The current value of this widget.|
|np_wheelItemCount|The number of items show in the selector wheel.|
Expand Down
115 changes: 111 additions & 4 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -62,6 +61,14 @@ public class NumberPicker extends LinearLayout {
public static final int ASCENDING = 0;
public static final int DESCENDING = 1;

@Retention(SOURCE)
@IntDef({LEFT, CENTER, RIGHT})
public @interface Align{}

public static final int RIGHT = 0;
public static final int CENTER = 1;
public static final int LEFT = 2;

/**
* The default update interval during long press.
*/
Expand Down Expand Up @@ -132,6 +139,11 @@ public class NumberPicker extends LinearLayout {
*/
private static final int DEFAULT_MIN_WIDTH = 64;

/**
* The default align of text.
*/
private static final int DEFAULT_TEXT_ALIGN = CENTER;

/**
* The default color of text.
*/
Expand Down Expand Up @@ -238,11 +250,36 @@ public static final Formatter getTwoDigitFormatter() {
*/
private final boolean mComputeMaxWidth;

/**
* The align of the selected text.
*/
private int mSelectedTextAlign = DEFAULT_TEXT_ALIGN;

/**
* The color of the selected text.
*/
private int mSelectedTextColor = DEFAULT_TEXT_COLOR;

/**
* The size of the selected text.
*/
private float mSelectedTextSize = DEFAULT_TEXT_SIZE;

/**
* Flag whether the selected text should strikethroughed.
*/
private boolean mSelectedTextStrikeThru;

/**
* Flag whether the selected text should underlined.
*/
private boolean mSelectedTextUnderline;

/**
* The align of the text.
*/
private int mTextAlign = DEFAULT_TEXT_ALIGN;

/**
* The color of the text.
*/
Expand All @@ -254,9 +291,14 @@ public static final Formatter getTwoDigitFormatter() {
private float mTextSize = DEFAULT_TEXT_SIZE;

/**
* The size of the selected text.
* Flag whether the text should strikethroughed.
*/
private float mSelectedTextSize = DEFAULT_TEXT_SIZE;
private boolean mTextStrikeThru;

/**
* Flag whether the text should underlined.
*/
private boolean mTextUnderline;

/**
* The typeface of the text.
Expand Down Expand Up @@ -662,13 +704,24 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mMaxValue = attributes.getInt(R.styleable.NumberPicker_np_max, mMaxValue);
mMinValue = attributes.getInt(R.styleable.NumberPicker_np_min, mMinValue);

mSelectedTextAlign = attributes.getInt(R.styleable.NumberPicker_np_selectedTextAlign,
mSelectedTextAlign);
mSelectedTextColor = attributes.getColor(R.styleable.NumberPicker_np_selectedTextColor,
mSelectedTextColor);
mSelectedTextSize = attributes.getDimension(R.styleable.NumberPicker_np_selectedTextSize,
spToPx(mSelectedTextSize));
mSelectedTextStrikeThru = attributes.getBoolean(
R.styleable.NumberPicker_np_selectedTextStrikeThru, mSelectedTextStrikeThru);
mSelectedTextUnderline = attributes.getBoolean(
R.styleable.NumberPicker_np_selectedTextUnderline, mSelectedTextUnderline);
mTextAlign = attributes.getInt(R.styleable.NumberPicker_np_textAlign, mTextAlign);
mTextColor = attributes.getColor(R.styleable.NumberPicker_np_textColor, mTextColor);
mTextSize = attributes.getDimension(R.styleable.NumberPicker_np_textSize,
spToPx(mTextSize));
mTextStrikeThru = attributes.getBoolean(
R.styleable.NumberPicker_np_textStrikeThru, mTextStrikeThru);
mTextUnderline = attributes.getBoolean(
R.styleable.NumberPicker_np_textUnderline, mTextUnderline);
mTypeface = Typeface.create(attributes.getString(R.styleable.NumberPicker_np_typeface),
Typeface.NORMAL);
mFormatter = stringToFormatter(attributes.getString(R.styleable.NumberPicker_np_formatter));
Expand Down Expand Up @@ -701,7 +754,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
// create the selector wheel paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextAlign(Align.CENTER);
paint.setTextAlign(Paint.Align.CENTER);
mSelectorWheelPaint = paint;

setSelectedTextColor(mSelectedTextColor);
Expand Down Expand Up @@ -1565,11 +1618,17 @@ protected void onDraw(Canvas canvas) {
int[] selectorIndices = getSelectorIndices();
for (int i = 0; i < selectorIndices.length; i++) {
if (i == mWheelMiddleItemIndex) {
mSelectorWheelPaint.setTextAlign(Paint.Align.values()[mSelectedTextAlign]);
mSelectorWheelPaint.setTextSize(mSelectedTextSize);
mSelectorWheelPaint.setColor(mSelectedTextColor);
mSelectorWheelPaint.setStrikeThruText(mSelectedTextStrikeThru);
mSelectorWheelPaint.setUnderlineText(mSelectedTextUnderline);
} else {
mSelectorWheelPaint.setTextAlign(Paint.Align.values()[mTextAlign]);
mSelectorWheelPaint.setTextSize(mTextSize);
mSelectorWheelPaint.setColor(mTextColor);
mSelectorWheelPaint.setStrikeThruText(mTextStrikeThru);
mSelectorWheelPaint.setUnderlineText(mTextUnderline);
}

int selectorIndex = selectorIndices[isAscendingOrder()
Expand Down Expand Up @@ -2341,6 +2400,10 @@ public void setScrollerEnabled(boolean scrollerEnabled) {
mScrollerEnabled = scrollerEnabled;
}

public void setSelectedTextAlign(@Align int align) {
mSelectedTextAlign = align;
}

public void setSelectedTextColor(@ColorInt int color) {
mSelectedTextColor = color;
mSelectedText.setTextColor(mSelectedTextColor);
Expand All @@ -2359,6 +2422,18 @@ public void setSelectedTextSize(@DimenRes int dimenId) {
setSelectedTextSize(getResources().getDimension(dimenId));
}

public void setSelectedTextStrikeThru(boolean strikeThruText) {
mSelectedTextStrikeThru = strikeThruText;
}

public void setSelectedTextUnderline(boolean underlineText) {
mSelectedTextUnderline = underlineText;
}

public void setTextAlign(@Align int align) {
mTextAlign = align;
}

public void setTextColor(@ColorInt int color) {
mTextColor = color;
mSelectorWheelPaint.setColor(mTextColor);
Expand All @@ -2377,6 +2452,14 @@ public void setTextSize(@DimenRes int dimenId) {
setTextSize(getResources().getDimension(dimenId));
}

public void setTextStrikeThru(boolean strikeThruText) {
mTextStrikeThru = strikeThruText;
}

public void setTextUnderline(boolean underlineText) {
mTextUnderline = underlineText;
}

public void setTypeface(Typeface typeface) {
mTypeface = typeface;
if (mTypeface != null) {
Expand Down Expand Up @@ -2455,6 +2538,10 @@ public boolean isScrollerEnabled() {
return mScrollerEnabled;
}

public int getSelectedTextAlign() {
return mSelectedTextAlign;
}

public int getSelectedTextColor() {
return mSelectedTextColor;
}
Expand All @@ -2463,6 +2550,18 @@ public float getSelectedTextSize() {
return mSelectedTextSize;
}

public boolean getSelectedTextStrikeThru() {
return mSelectedTextStrikeThru;
}

public boolean getSelectedTextUnderline() {
return mSelectedTextUnderline;
}

public int getTextAlign() {
return mTextAlign;
}

public int getTextColor() {
return mTextColor;
}
Expand All @@ -2471,6 +2570,14 @@ public float getTextSize() {
return spToPx(mTextSize);
}

public boolean getTextStrikeThru() {
return mTextStrikeThru;
}

public boolean getTextUnderline() {
return mTextUnderline;
}

public Typeface getTypeface() {
return mTypeface;
}
Expand Down
14 changes: 14 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@
<enum name="vertical" value="1" />
</attr>
<attr name="np_scrollerEnabled" format="boolean" />
<attr name="np_selectedTextAlign" format="enum">
<enum name="selectedTextAlignRight" value="0" />
<enum name="selectedTextAlignCenter" value="1" />
<enum name="selectedTextAlignLeft" value="2" />
</attr>
<attr name="np_selectedTextColor" format="color" />
<attr name="np_selectedTextSize" format="dimension" />
<attr name="np_selectedTextStrikeThru" format="boolean" />
<attr name="np_selectedTextUnderline" format="boolean" />
<attr name="np_textAlign" format="enum">
<enum name="textAlignRight" value="0" />
<enum name="textAlignCenter" value="1" />
<enum name="textAlignLeft" value="2" />
</attr>
<attr name="np_textColor" format="color" />
<attr name="np_textSize" format="dimension" />
<attr name="np_textStrikeThru" format="boolean" />
<attr name="np_textUnderline" format="boolean" />
<attr name="np_typeface" format="string" />
<attr name="np_value" format="integer" />
<attr name="np_wheelItemCount" format="integer" />
Expand Down

0 comments on commit 1b1966b

Please sign in to comment.