Skip to content

Commit

Permalink
Support textStyle (#50)
Browse files Browse the repository at this point in the history
* Support textStyle

* reset longestcharwidth in invalidate

* Use typeface approach to getting bold / italic instead

* Use default typeface on paint
  • Loading branch information
jinatonic authored and danh32 committed Apr 28, 2017
1 parent 1e399cd commit 976e776
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions ticker-sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
android:gravity="center"
android:padding="@dimen/activity_vertical_margin"
android:textAppearance="@style/TickerTextAppearance"
android:textStyle="bold"
app:ticker_animationDuration="500" />

<com.robinhood.ticker.TickerView
Expand Down
1 change: 1 addition & 0 deletions ticker-sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<style name="TickerTextAppearance">
<item name="android:textColor">?colorPrimary</item>
<item name="android:textSize">50sp</item>
<item name="android:textStyle">bold|italic</item>
</style>

</resources>
19 changes: 17 additions & 2 deletions ticker/src/main/java/com/robinhood/ticker/TickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ public class TickerView extends View {
private int lastMeasuredDesiredWidth, lastMeasuredDesiredHeight;

// View attributes, defaults are set in init().
private float textSize;
private int gravity;
private int textColor;
private float textSize;
private int textStyle;
private long animationDurationInMillis;
private Interpolator animationInterpolator;
private int gravity;
private boolean animateMeasurementChange;

public TickerView(Context context) {
Expand Down Expand Up @@ -149,6 +150,10 @@ protected void init(Context context, AttributeSet attrs, int defStyleAttr, int d
textPaint.setShadowLayer(styledAttributes.shadowRadius, styledAttributes.shadowDx,
styledAttributes.shadowDy, styledAttributes.shadowColor);
}
if (styledAttributes.textStyle != 0) {
textStyle = styledAttributes.textStyle;
setTypeface(textPaint.getTypeface());
}

setTextColor(styledAttributes.textColor);
setTextSize(styledAttributes.textSize);
Expand Down Expand Up @@ -180,6 +185,7 @@ private class StyledAttributes {
float shadowRadius;
int textColor;
float textSize;
int textStyle;

StyledAttributes(Resources res) {
textColor = DEFAULT_TEXT_COLOR;
Expand All @@ -198,6 +204,7 @@ void applyTypedArray(TypedArray arr) {
shadowRadius);
textColor = arr.getColor(R.styleable.ticker_TickerView_android_textColor, textColor);
textSize = arr.getDimension(R.styleable.ticker_TickerView_android_textSize, textSize);
textStyle = arr.getInt(R.styleable.ticker_TickerView_android_textStyle, textStyle);
}
}

Expand Down Expand Up @@ -338,6 +345,14 @@ public Typeface getTypeface() {
* @param typeface the typeface to use on the text.
*/
public void setTypeface(Typeface typeface) {
if (textStyle == Typeface.BOLD_ITALIC) {
typeface = Typeface.create(typeface, Typeface.BOLD_ITALIC);
} else if (textStyle == Typeface.BOLD) {
typeface = Typeface.create(typeface, Typeface.BOLD);
} else if (textStyle == Typeface.ITALIC) {
typeface = Typeface.create(typeface, Typeface.ITALIC);
}

textPaint.setTypeface(typeface);
onTextPaintMeasurementChanged();
}
Expand Down
1 change: 1 addition & 0 deletions ticker/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<attr name="android:textAppearance" tools:ignore="ResourceName" />
<attr name="android:textColor" tools:ignore="ResourceName" />
<attr name="android:textSize" tools:ignore="ResourceName" />
<attr name="android:textStyle" tools:ignore="ResourceName" />
</declare-styleable>
</resources>

0 comments on commit 976e776

Please sign in to comment.