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. A base path to font files may be specified via
setBasePath(String) which will be used by the Font class when building full path to a
specific .ttf file whenever its Typeface needs to be created. When base path is used all
paths to .ttf files specified via uiFont may be relative in association to
that base path. 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, by default, placed all
custom font files.
|
static String |
TTF_SUFFIX
Suffix for original true Typeface fonts.
|
| Constructor and Description |
|---|
Font(String filePath)
Creates a new instance of Font with the specified path that points to a .ttf file within assets
folder.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
clearCache()
Clears the current cache with already created and cached Font instances.
|
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 filePath)
Creates an instance of Font with the given file path.
|
String |
getFilePath()
Returns the path to the font file that is represented by 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.
|
static void |
setBasePath(String basePath)
Specifies a base path at which are stored font .ttf files that are to be created via
create(String) and cached by this Font class. |
static void |
setLogLevel(int level)
Sets a logging level determining at which level should be visible messages logged by Font class
into console via
Log utility. |
public static final String TTF_SUFFIX
Constant value: .ttf
public static final String FONT_DIR
However, this directory may contain custom sub-directories in order to group related fonts together.
Constant value: font/
setBasePath(String)public Font(@NonNull
String filePath)
Consider using create(String) instead which may return already created and cached
instance of Font.
Note that empty or null paths are not allowed.
Only .ttf font files are supported!
filePath - 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 filePath is empty.public static void setLogLevel(int level)
Log utility.
Note that the current implementation of Font class logs only messages at
Log.DEBUG level.
Default value: Log.ASSERT
level - The desired log level.public static void setBasePath(@Nullable
String basePath)
create(String) and cached by this Font class.
The specified path is used to build a full path to a specific font file whenever its representing
Typeface needs to be created via Typeface.createFromAsset(AssetManager, String).
Default value: FONT_DIR
basePath - The desired base path within Android assets folder. May be null to not
use any base path, when in such case the uiFont attribute
or path specified via create(String) should always contain a full path
to a desired font file. May be null to indicate that the default path
should be used.@Nullable public static Font create(@NonNull android.content.Context context, @Nullable android.util.AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes 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)@Nullable public static Font create(@NonNull android.content.Context context, @StyleRes 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)@NonNull public static Font create(@NonNull String filePath)
IllegalArgumentException - If the given filePath is empty.create(Context, AttributeSet, int, int),
create(Context, int),
Font(String)public static void clearCache()
Note that this will not invalidate those instances as they may be in use across the application.
@NonNull public String getFilePath()
create(String),
setBasePath(String)@NonNull
public android.graphics.Typeface getTypeface(@NonNull
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 it is not a valid .ttf file.Typeface.createFromAsset(android.content.res.AssetManager, String)