Search in sources :

Example 1 with Typeface

use of android.graphics.Typeface in project Calligraphy by chrisjenx.

the class CalligraphyFactory method onViewCreatedInternal.

void onViewCreatedInternal(View view, final Context context, AttributeSet attrs) {
    if (view instanceof TextView) {
        // TextView's inside the Toolbar/ActionBar).
        if (TypefaceUtils.isLoaded(((TextView) view).getTypeface())) {
            return;
        }
        // Try to get typeface attribute value
        // Since we're not using namespace it's a little bit tricky
        // Check xml attrs, style attrs and text appearance for font path
        String textViewFont = resolveFontPath(context, attrs);
        // Try theme attributes
        if (TextUtils.isEmpty(textViewFont)) {
            final int[] styleForTextView = getStyleForTextView((TextView) view);
            if (styleForTextView[1] != -1)
                textViewFont = CalligraphyUtils.pullFontPathFromTheme(context, styleForTextView[0], styleForTextView[1], mAttributeId);
            else
                textViewFont = CalligraphyUtils.pullFontPathFromTheme(context, styleForTextView[0], mAttributeId);
        }
        // Still need to defer the Native action bar, appcompat-v7:21+ uses the Toolbar underneath. But won't match these anyway.
        final boolean deferred = matchesResourceIdName(view, ACTION_BAR_TITLE) || matchesResourceIdName(view, ACTION_BAR_SUBTITLE);
        CalligraphyUtils.applyFontToTextView(context, (TextView) view, CalligraphyConfig.get(), textViewFont, deferred);
    }
    // Toolbar(Which underlies the ActionBar) for its children.
    if (CalligraphyUtils.canCheckForV7Toolbar() && view instanceof android.support.v7.widget.Toolbar) {
        final Toolbar toolbar = (Toolbar) view;
        toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ToolbarLayoutListener(this, context, toolbar));
    }
    // Try to set typeface for custom views using interface method or via reflection if available
    if (view instanceof HasTypeface) {
        Typeface typeface = getDefaultTypeface(context, resolveFontPath(context, attrs));
        if (typeface != null) {
            ((HasTypeface) view).setTypeface(typeface);
        }
    } else if (CalligraphyConfig.get().isCustomViewTypefaceSupport() && CalligraphyConfig.get().isCustomViewHasTypeface(view)) {
        final Method setTypeface = ReflectionUtils.getMethod(view.getClass(), "setTypeface");
        String fontPath = resolveFontPath(context, attrs);
        Typeface typeface = getDefaultTypeface(context, fontPath);
        if (setTypeface != null && typeface != null) {
            ReflectionUtils.invokeMethod(view, setTypeface, typeface);
        }
    }
}
Also used : Toolbar(android.support.v7.widget.Toolbar) Typeface(android.graphics.Typeface) TextView(android.widget.TextView) Method(java.lang.reflect.Method) Toolbar(android.support.v7.widget.Toolbar)

Example 2 with Typeface

use of android.graphics.Typeface in project Calligraphy by chrisjenx.

the class CalligraphyTypefaceSpan method apply.

private void apply(final Paint paint) {
    final Typeface oldTypeface = paint.getTypeface();
    final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
    final int fakeStyle = oldStyle & ~typeface.getStyle();
    if ((fakeStyle & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }
    if ((fakeStyle & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }
    paint.setTypeface(typeface);
}
Also used : Typeface(android.graphics.Typeface) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 3 with Typeface

use of android.graphics.Typeface in project Calligraphy by chrisjenx.

the class CalligraphyUtils method applyFontToTextView.

static boolean applyFontToTextView(final Context context, final TextView textView, final String filePath, boolean deferred) {
    if (textView == null || context == null)
        return false;
    final AssetManager assetManager = context.getAssets();
    final Typeface typeface = TypefaceUtils.load(assetManager, filePath);
    return applyFontToTextView(textView, typeface, deferred);
}
Also used : AssetManager(android.content.res.AssetManager) Typeface(android.graphics.Typeface)

Example 4 with Typeface

use of android.graphics.Typeface in project cw-android by commonsguy.

the class FontSampler method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    TextView tv = (TextView) findViewById(R.id.custom);
    Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
    tv.setTypeface(face);
    File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");
    if (font.exists()) {
        tv = (TextView) findViewById(R.id.file);
        face = Typeface.createFromFile(font);
        tv.setTypeface(face);
    } else {
        findViewById(R.id.filerow).setVisibility(View.GONE);
    }
}
Also used : Typeface(android.graphics.Typeface) TextView(android.widget.TextView) File(java.io.File)

Example 5 with Typeface

use of android.graphics.Typeface in project CustomFontLib by daniribalbert.

the class CustomFontUtils method getTypeFace.

public static Typeface getTypeFace(final TextView v, final AttributeSet attrs) {
    final TypedArray tArray = v.getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.customFont, 0, 0);
    String fontName = "";
    try {
        fontName = tArray.getString(R.styleable.customFont_font);
    } finally {
        tArray.recycle();
    }
    if (!TextUtils.isEmpty(fontName)) {
        final int style;
        final Typeface typeface = v.getTypeface();
        if (typeface == null) {
            style = Typeface.NORMAL;
        } else {
            style = typeface.getStyle();
        }
        String mFontName = getFontFile(v.getContext(), fontName, style);
        if (sTypefaceMap.containsKey(mFontName)) {
            return sTypefaceMap.get(mFontName);
        }
        Typeface newTypeface = typeface;
        if (!TextUtils.isEmpty(mFontName)) {
            try {
                newTypeface = Typeface.createFromAsset(v.getContext().getAssets(), FONTS_DIR + "/" + mFontName);
            } catch (Exception e) {
                newTypeface = typeface;
            }
        }
        sTypefaceMap.put(mFontName, newTypeface);
        return newTypeface;
    }
    return null;
}
Also used : Typeface(android.graphics.Typeface) TypedArray(android.content.res.TypedArray)

Aggregations

Typeface (android.graphics.Typeface)263 Paint (android.graphics.Paint)56 TextPaint (android.text.TextPaint)40 TypedArray (android.content.res.TypedArray)34 TextView (android.widget.TextView)21 View (android.view.View)20 Canvas (android.graphics.Canvas)15 Bitmap (android.graphics.Bitmap)13 Context (android.content.Context)11 Resources (android.content.res.Resources)10 Test (org.junit.Test)8 ColorStateList (android.content.res.ColorStateList)7 Legend (com.github.mikephil.charting.components.Legend)7 File (java.io.File)7 Intent (android.content.Intent)6 Attributes (com.cengalabs.flatui.Attributes)6 XAxis (com.github.mikephil.charting.components.XAxis)6 YAxis (com.github.mikephil.charting.components.YAxis)6 ContentResolver (android.content.ContentResolver)5 Drawable (android.graphics.drawable.Drawable)5