/**
 * Copyright (C) 2012 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.components;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Typeface;

import android.util.AttributeSet;
import android.util.Log;

import android.widget.Button;

import ch.spherIC.recurvebowsight.R;


/**
 * Siehe http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android.
 *
 * @author  FC Smilari
 */
public class FontButton extends Button {

    private static final String TAG = "Button";

    public FontButton(final Context context) {
        super(context);
    }

    public FontButton(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        setBtnFont(context, attrs);
    }

    public FontButton(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        setBtnFont(context, attrs);
    }

    private void setBtnFont(final Context ctx, final AttributeSet attrs) {
        if (!isInEditMode()) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontButton);
            String font = a.getString(R.styleable.FontButton_btnFont);
            if (font != null && !font.isEmpty()) {
                setBtnFont(ctx, font);
            }
            a.recycle();
        }
    }

    public boolean setBtnFont(final Context ctx, final String font) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), font);
        } catch (final Exception e) {
            Log.e(TAG, "Could not get typeface: " + e.getMessage());
            return false;
        }

        setTypeface(tf);
        return true;
    }


}
