Skip to content

Commit 96cd3b1

Browse files
committed
2 parents 82ea168 + a4573bc commit 96cd3b1

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ Typeface typeface = RobotoTypefaceManager.obtainTypeface(
114114
RobotoTextViewUtils.setTypeface(textView, typeface);
115115
```
116116

117+
#### With Span
118+
119+
Using parameter `typeface`:
120+
``` java
121+
RobotoTypefaceSpan span = new RobotoTypefaceSpan(
122+
context,
123+
RobotoTypefaceManager.Typeface.ROBOTO_BOLD);
124+
Spannable spannable = new SpannableString("text");
125+
spannable.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
126+
```
127+
128+
Using parameters `fontFamily`, `textWeight` and `textStyle`:
129+
``` java
130+
RobotoTypefaceSpan span = new RobotoTypefaceSpan(
131+
context,
132+
RobotoTypefaceManager.FontFamily.ROBOTO,
133+
RobotoTypefaceManager.TextWeight.LIGHT,
134+
RobotoTypefaceManager.TextStyle.ITALIC);
135+
Spannable spannable = new SpannableString("text");
136+
spannable.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
137+
```
117138

118139
Gradle
119140
------
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.devspark.robototextview.style;
2+
3+
import android.content.Context;
4+
import android.graphics.Typeface;
5+
import android.text.TextPaint;
6+
import android.text.style.MetricAffectingSpan;
7+
8+
import com.devspark.robototextview.util.RobotoTextViewUtils;
9+
import com.devspark.robototextview.util.RobotoTypefaceManager;
10+
11+
/**
12+
* Span for replacing typeface
13+
*/
14+
public class RobotoTypefaceSpan extends MetricAffectingSpan {
15+
16+
/**
17+
* Created typefaces.
18+
*/
19+
private final Typeface mTypeface;
20+
21+
/**
22+
* Constructor to use with default typeface (regular).
23+
*
24+
* @param context The Context the span is using in, through which it can access the current theme, resources, etc.
25+
*/
26+
public RobotoTypefaceSpan(Context context) {
27+
this(context, RobotoTypefaceManager.Typeface.ROBOTO_REGULAR);
28+
}
29+
30+
/**
31+
* Constructor to use with Typeface Id.
32+
*
33+
* @param context The Context the span is using in, through which it can access the current theme, resources, etc.
34+
* @param typefaceId Typeface Id ({@link com.devspark.robototextview.util.RobotoTypefaceManager.Typeface})
35+
*/
36+
public RobotoTypefaceSpan(Context context, int typefaceId) {
37+
mTypeface = RobotoTypefaceManager.obtainTypeface(context, typefaceId);
38+
}
39+
40+
/**
41+
* Constructor to use with set of parameters
42+
*
43+
* @param context The Context the span is using in, through which it can access the current theme, resources, etc.
44+
* @param fontFamily The value of "fontFamily" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.FontFamily})
45+
* @param textWeight The value of "textWeight" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.TextWeight})
46+
* @param textStyle The value of "textStyle" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.TextStyle})
47+
*/
48+
public RobotoTypefaceSpan(Context context, int fontFamily, int textWeight, int textStyle) {
49+
mTypeface = RobotoTypefaceManager.obtainTypeface(context, fontFamily, textWeight, textStyle);
50+
}
51+
52+
@Override
53+
public void updateDrawState(TextPaint ds) {
54+
RobotoTextViewUtils.setTypeface(ds, mTypeface);
55+
}
56+
57+
@Override
58+
public void updateMeasureState(TextPaint paint) {
59+
RobotoTextViewUtils.setTypeface(paint, mTypeface);
60+
}
61+
}

robototextview/src/main/java/com/devspark/robototextview/util/RobotoTextViewUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ public static void setTypeface(TextView textView, Typeface typeface) {
8181
textView.setTypeface(typeface);
8282
}
8383

84+
/**
85+
* Setup typeface for Paint.
86+
*
87+
* @param paint The paint
88+
* @param typeface The specify typeface
89+
*/
90+
public static void setTypeface(Paint paint, Typeface typeface) {
91+
paint.setFlags(paint.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
92+
paint.setTypeface(typeface);
93+
}
94+
8495
}

0 commit comments

Comments
 (0)