The goal of this library is to allow your app to support multiple FontFamilies (e.g. lato, roboto, etc.) with their own styles (e.g. normal, bold, italic) in an easily-configurable way.
repositories {
mavenCentral()
}
dependencies {
compile('com.guardanis:font-utils:1.0.4')
}
First, make sure your fonts are located in your assets folder (in a /fonts/ subdirectory to be organized). Let's assume you have a roboto normal/bold font called robo.ttf and robo_bold.ttf, respectively. Now you need to override the R.array.fu__font_family_resources string-array resource to include your fonts:
<string-array name="fu__font_family_resources">
<item>roboto--normal=fonts/robo.ttf,bold=fonts/robo_bold.ttf</item>
</string-array>
The format here is:
<item>FONT_FAMILY_NAME--STYLE_1=FONT1_PATH,STYLE_2=FONT2_PATH</item>
With the supported styles being
- NORMAL
- BOLD
- ITALICS
- BOLD_ITALICS
Values in the resources are not case-sensitive, and their comparing integers are static in the Typeface class (e.g. Typeface.BOLD or Typeface.ITALICS).
You can also use additional custom typeface style variants by assigning them an integer value, instead of using one of the accepted Typeface values:
<string-array name="fu__font_family_resources">
<item>roboto--normal=fonts/robo.ttf,123456=fonts/robo_bold.ttf</item>
</string-array>
Now, we need to ensure we have a default FontFamily correctly set by overring R.string.fu__default_font_family:
<string name="fu__default_font_family">roboto</string>
Next, we have 3 choices for how to use this font. We can either
<com.guardanis.fontutils.TextView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:textFontFamily="roboto"
android:textStyle="bold" />
These convenience helper classes include
- com.guardanis.fontutils.TextView
- com.guardanis.fontutils.Button
- com.guardanis.fontutils.EditText
Check them out if you want to implement them for other types of TextViews.
Call the following from your custom TextView (or TextView subclass)'s constructor.
FontUtils.getInstance(getContext())
.load(this, attrs, defStyle);
TextView view = (TextView) findViewById(R.id.some_id);
FontUtils.getInstance(getContext())
.setTypeface(view, fontFamily, style);
- A NORMAL font is required for a typeface to be loaded. If the FontFamily is loaded without a normal font, it will throw a RuntimeException letting you know what went wrong.
- Trying to load any non-normal font style that doesn't exist will default to the normal style.
As of version 1.0.4, font-utils will be hosted on MavenCentral. Versions 1.0.3 and below will remain on JCenter.