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

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


/**
 * Siehe http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android.
 *
 * @author FC Smilari
 */
@SuppressLint("AppCompatCustomView")
public class FontTextView extends TextView {

    private static final String TAG = "TextView";

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

    public FontTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        setFont(context, attrs);
    }

    public FontTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        setFont(context, attrs);
    }

    private void setFont(final Context ctx, final AttributeSet attrs) {
        /*if (!isInEditMode()) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView);
            String font = a.getString(R.styleable.FontTextView_font);
            if (font != null && !font.isEmpty()) {
                setFont(ctx, font);
            }
            a.recycle();
        }*/
    }

    public boolean setFont(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;
    }


}
