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);
}
}
}
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);
}
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);
}
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);
}
}
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;
}
Aggregations