public final class Font extends Object
Typeface object. Each Font instance is
required to be created with a relative path to its corresponding .ttf font file that is placed
within assets directory. A new Font instance may be created directly via Font(String)
constructor, however it is encouraged to create new instances of Font via one of create(String),
create(Context, int) or create(Context, AttributeSet, int, int) factory methods,
because these methods ensure that already created fonts are reused across entire Android application
which definitely improves performance as creation of a single Typeface object via
Typeface.createFromAsset(AssetManager, String) is a costly operation.
This library supports only .ttf font files. Each font file is required to be placed within
assets directory of the corresponding application. If FontConfig.STRICT_STORAGE is enabled,
then font files must be placed in assets/font/ directory, but may be grouped in sub-directories.
Relative path to a specific true typeface font file may or may not have specified .ttf
suffix. If the suffix is not specified, this class ads such suffix automatically.
Typeface object of a specific Font instance may be obtained via getTypeface(Context).
| Modifier and Type | Field and Description |
|---|---|
static String |
FONT_DIR
Sub-directory within an application assets folder, where should be placed all custom font files
if strict mode is enabled via
FontConfig.STRICT_STORAGE. |
static String |
TTF_SUFFIX
Suffix for original true Typeface fonts.
|
| Constructor and Description |
|---|
Font(String fontPath)
Creates a new instance of Font with the specified path to font file within assets folder.
|
| Modifier and Type | Method and Description |
|---|---|
static Font |
create(android.content.Context context,
android.util.AttributeSet attrs,
int defStyleAttr,
int defStyleRes)
Creates a new instance of Font with the font path obtained from the given attrs set.
|
static Font |
create(android.content.Context context,
int style)
Creates a new instance of Font with the font path obtained from the given style.
|
static Font |
create(String fontPath)
Creates an instance of Font with the given font path.
|
String |
getFontPath()
Returns the path to the font file of this font instance.
|
android.graphics.Typeface |
getTypeface(android.content.Context context)
Returns instance of the Typeface which is created for the .ttf file of this font instance.
|
public static final String TTF_SUFFIX
Constant value: .ttf
public static final String FONT_DIR
FontConfig.STRICT_STORAGE.
This folder however may contain custom sub-directories to group related fonts together.
Constant value: font/
public Font(String fontPath)
Note, that empty path or null are not valid arguments.
Only .ttf font files are supported!
fontPath - Relative path to the .ttf file (with or without .ttf suffix) placed within
an application assets directory which will be represented by the newly created
Font instance.IllegalArgumentException - If the given fontPath is empty.public static Font create(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
context - Context used to process the given attributes set.attrs - Attributes set which that is expected to have uiFont
attribute specified with font path.defStyleAttr - An attribute of the default style presented within the current theme which
supplies default attributes for TypedArray.defStyleRes - Resource id of the default style used when the defStyleAttr is 0
or the current theme does not have style for defStyleAttr specified.null if font path parsed from the given attributes
set is invalid.create(String),
create(Context, int)public static Font create(android.content.Context context, int style)
context - Context used to process the given style.style - Resource id of the desired style that is expected to have uiFont
attribute specified with font path.null if font path parsed from the given style
is invalid.create(String),
create(Context, AttributeSet, int, int)public static Font create(String fontPath)
IllegalArgumentException - If the given fontPath is empty.create(Context, AttributeSet, int, int),
create(Context, int),
Font(String)public String getFontPath()
Font(String)public android.graphics.Typeface getTypeface(android.content.Context context)
Subsequent calls to this method will return already created and cached Typeface object.
context - Valid context used to create requested TypeFace.IllegalArgumentException - If font file at the path specified for this font does not
exist or is not a valid .ttf file.Typeface.createFromAsset(android.content.res.AssetManager, String)