forked from mafshin/CustomTextView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTextView.java
More file actions
50 lines (43 loc) · 1.51 KB
/
CustomTextView.java
File metadata and controls
50 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.my.app;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import com.my.app.R // your package resource name to reference the attribitures
/**
* Created with IntelliJ IDEA.
* User: Mohsen Afshin
* Date: 8/4/13
* Time: 5:36 PM
*/
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyAttributes(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyAttributes(context, attrs);
}
private void applyAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CustomTextView_fontAssetName:
try {
Typeface font = Typeface.createFromAsset(getResources().getAssets(), a.getString(attr));
if (font != null) {
this.setTypeface(font);
}
} catch (RuntimeException e) {
}
}
}
}
}