首页 > 代码库 > 自定义TextView 调用ttf格式字体

自定义TextView 调用ttf格式字体

自定义TextView 调用ttf格式字体

<strong>将ttf格式文件存放在assets/fonts/下</strong>

注:PC系统字体存放在C:\Windows\Fonts下

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 修改字体
 *
 */
public class MyTextView extends TextView
{

    public MyTextView(Context context)
    {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        changeTypeFace(context, attrs);
    }

    /**
     * 改变字体类型
     * @param context
     * @param attrs
     */
    private void changeTypeFace(Context context, AttributeSet attrs)
    {
        if (attrs != null)
        {
            //TypedArray a = context.obtainStyledAttributes(attrs,
            //R.styleable.TextView_Typefaces);
            //            tf = a.getInt(R.styleable.TextView_Typefaces_tf, tf);
            Typeface mtf = Typeface.createFromAsset(context.getAssets(),
                    "fonts/UnidreamLED.ttf");
            super.setTypeface(mtf);
        }
    }
}