Skip to content

Commit

Permalink
Support custom attribute in textAppearance
Browse files Browse the repository at this point in the history
Preserves support for android:fontFamily
  • Loading branch information
loganj authored and chrisjenx committed May 29, 2014
1 parent d426332 commit 57b040c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
6 changes: 2 additions & 4 deletions CalligraphySample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@

<style name="AppThemeWidgetTextViewAppearanceStyle">
<item name="android:textAppearance">@style/TextAppearance.FontFamily</item>
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>

<style name="TextAppearance.FontFamily" parent="android:TextAppearance">
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
<item name="android:fontFamily">fonts/RobotoCondensed-Regular.ttf</item>
<item name="android:textColor">#444</item>
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
<item name="android:textColor">#444</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,42 @@ static String pullFontPathFromStyle(Context context, AttributeSet attrs, int att
typedArray.recycle();
}
}
return pullFontPathFromTextAppearance(context, attrs);
return pullFontPathFromTextAppearance(context, attrs, attributeId);
}

static final String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs) {
if (R_Styleable_TextView == null || R_Styleable_TextAppearance == null
|| R_Styleable_TextView_textAppearance == -1 || R_Styleable_TextAppearance_fontFamily == -1) {
static final String pullFontPathFromTextAppearance(final Context context, AttributeSet attrs, int attributeId) {
boolean usingFontFamily = attributeId == android.R.attr.fontFamily;

if (R_Styleable_TextView == null
|| R_Styleable_TextAppearance == null
|| R_Styleable_TextView_textAppearance == -1
|| (usingFontFamily && attributeId == -1)) {
return null;
}

TypedArray textViewAttrs = context.obtainStyledAttributes(attrs, R_Styleable_TextView);
if (textViewAttrs == null) {
return null;
}

int textAppearanceId = textViewAttrs.getResourceId(R_Styleable_TextView_textAppearance, -1);
TypedArray textAppearanceAttrs = null;
try {
if (usingFontFamily) {
textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId, R_Styleable_TextAppearance);
if (textAppearanceAttrs == null) {
return null;
}
return textAppearanceAttrs.getString(R_Styleable_TextAppearance_fontFamily);
} else {
textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId, new int[] { attributeId });
return textAppearanceAttrs.getString(0);
}

TypedArray textViewAttrs = context.obtainStyledAttributes(attrs, R_Styleable_TextView);
if (textViewAttrs == null) {
return null;
} finally {
if (textAppearanceAttrs != null) {
textAppearanceAttrs.recycle();
}

int textAppearanceId = textViewAttrs.getResourceId(R_Styleable_TextView_textAppearance, -1);
TypedArray textAppearanceAttrs = context.obtainStyledAttributes(textAppearanceId, R_Styleable_TextAppearance);
if (textAppearanceAttrs == null) {
return null;
}

return textAppearanceAttrs.getString(R_Styleable_TextAppearance_fontFamily);
}
}

static String pullFontPathFromTheme(Context context, int styleId, int attributeId) {
Expand Down

0 comments on commit 57b040c

Please sign in to comment.