UTextView.javaattr.xmlsource+
public class UTextView extends android.support.v7.widget.AppCompatTextView {
private String defaultFont="iransans.ttf";
public UTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public UTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public UTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs!=null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_fontName);
if (fontName!=null) {
try {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/"+fontName);
setTypeface(myTypeface);
}
catch (Exception e) {}
}
a.recycle();
}else{
try {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/"+defaultFont);
setTypeface(myTypeface);
}
catch (Exception e) {}
}
}
}
<resources>
<declare-styleable name="CustomTextView">
<attr name="fontName" format="string" />
</declare-styleable>
</resources>
<com.framework.view.UTextView
android:id="@+id/contactus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="7dp"
android:gravity="right|top"
android:lineSpacingMultiplier="1.5"
android:text="test font"
app:fontName="1.ttf" />