Created
November 20, 2014 11:01
-
-
Save robUx4/4ccf40047f5f7a9e64f6 to your computer and use it in GitHub Desktop.
Basic Font setter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.levelup.touiteur; | |
import android.graphics.Typeface; | |
import android.util.SparseArray; | |
import android.widget.TextView; | |
import com.levelup.touiteur.log.TouiteurLog; | |
public class FontManager { | |
public enum Font { | |
roboto, | |
robotoLight, | |
robotoSlab, | |
robotoBold, | |
robotoMedium, | |
} | |
private final SparseArray<Typeface> mFonts = new SparseArray<Typeface>(5); | |
/** | |
* Do not call directly, use {@link Touiteur#getFontManager()} | |
*/ | |
public FontManager() { | |
} | |
public Typeface getFont(Font font) { | |
if (!UserPreferences.getInstance().getBoolean(UserPreferences.UseSystemFont)) { | |
// add the font from storage as we're not going to use it | |
Typeface result = mFonts.get(font.ordinal(), null); | |
if (null==result) { | |
result = loadFont(font); | |
mFonts.append(font.ordinal(), result); | |
} | |
return result; | |
} else { | |
// remove the font from storage as we're not going to use it | |
mFonts.remove(font.ordinal()); | |
} | |
return null; | |
} | |
private Typeface loadFont(Font font) { | |
final String fontPath; | |
switch (font) { | |
case roboto: | |
fontPath = "fonts/Roboto-Regular.ttf"; | |
break; | |
case robotoLight: | |
fontPath = "fonts/Roboto-Light.ttf"; | |
break; | |
case robotoBold: | |
fontPath = "fonts/Roboto-Bold.ttf"; | |
break; | |
case robotoSlab: | |
fontPath = "fonts/RobotoSlab-Regular.ttf"; | |
break; | |
case robotoMedium: | |
fontPath = "fonts/Roboto-Medium.ttf"; | |
break; | |
default: | |
fontPath = null; | |
break; | |
} | |
if (null!=fontPath) | |
try { | |
return Typeface.createFromAsset(Touiteur.sApp.getAssets(), fontPath); | |
} catch (Exception e) { | |
TouiteurLog.e(false, "Error creating font "+fontPath, e); | |
} | |
final String systemName = getDefaultName(font); | |
if (null!=systemName) | |
try { | |
return Typeface.create(systemName, Typeface.NORMAL); | |
} catch (Exception e) { | |
TouiteurLog.e(false, "Error creating font "+systemName, e); | |
} | |
return null; | |
} | |
public void setTypeface(Font font, TextView textview) { | |
final Typeface tf = getFont(font); | |
if (null!=tf && null!=textview) | |
textview.setTypeface(tf); | |
} | |
public static String getDefaultName(Font font) { | |
switch (font) { | |
case roboto: | |
return "sans-serif"; | |
case robotoLight: | |
return "sans-serif-light"; | |
case robotoBold: | |
return "sans-serif-bold"; | |
case robotoSlab: | |
return "sans-serif"; | |
case robotoMedium: | |
return "sans-serif-medium"; | |
default: | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment