FontFamily
public
final
class
FontFamily
extends Object
A font family class can be used for creating Typeface.
A font family is a bundle of fonts for drawing text in various styles.
For example, you can bundle regular style font and bold style font into a single font family,
then system will select the correct style font from family for drawing.
FontFamily family = new FontFamily.Builder(new Font.Builder("regular.ttf").build())
.addFont(new Font.Builder("bold.ttf").build()).build();
Typeface typeface = new Typeface.Builder2(family).build();
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello, World.");
ssb.setSpan(new StyleSpan(Typeface.Bold), 6, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setTypeface(typeface);
textView.setText(ssb);
In this example, "Hello, " is drawn with "regular.ttf", and "World." is drawn with "bold.ttf".
If there is no font exactly matches with the text style, the system will select the closest font.
Summary
Nested classes |
class |
FontFamily.Builder
A builder class for creating new FontFamily.
|
Public methods |
Font
|
getFont(int index)
Returns a font
|
int
|
getSize()
Returns the number of fonts in this FontFamily.
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long timeoutMillis, int nanos)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait(long timeoutMillis)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted.
|
|
Public methods
getFont
public Font getFont (int index)
Returns a font
Parameters |
index |
int : an index of the font
Value is 0 or greater |
Returns |
Font |
a registered font
This value cannot be null . |
getSize
public int getSize ()
Returns the number of fonts in this FontFamily.
Returns |
int |
the number of fonts registered in this family.
Value is 1 or greater |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# FontFamily\n\nAdded in [API level 29](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Nested Classes](#nestedclasses) \\| [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nFontFamily\n==========\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/graphics/fonts/FontFamily \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\nfinal\n\nclass\nFontFamily\n`\n\n\n`\n\nextends `[Object](/reference/java/lang/Object)`\n\n\n`\n\n`\n\n\n`\n\n|---|-----------------------------------|\n| [java.lang.Object](/reference/java/lang/Object) ||\n| ↳ | android.graphics.fonts.FontFamily |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA font family class can be used for creating Typeface.\n\n\nA font family is a bundle of fonts for drawing text in various styles.\nFor example, you can bundle regular style font and bold style font into a single font family,\nthen system will select the correct style font from family for drawing.\n\n```\n FontFamily family = new FontFamily.Builder(new Font.Builder(\"regular.ttf\").build())\n .addFont(new Font.Builder(\"bold.ttf\").build()).build();\n Typeface typeface = new Typeface.Builder2(family).build();\n\n SpannableStringBuilder ssb = new SpannableStringBuilder(\"Hello, World.\");\n ssb.setSpan(new StyleSpan(Typeface.Bold), 6, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);\n\n textView.setTypeface(typeface);\n textView.setText(ssb);\n \n```\nIn this example, \"Hello, \" is drawn with \"regular.ttf\", and \"World.\" is drawn with \"bold.ttf\". If there is no font exactly matches with the text style, the system will select the closest font.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nSummary\n-------\n\n| ### Nested classes ||\n|----------|-------------------------------------------------------------------------------------------------------------------------|\n| ` class` | [FontFamily.Builder](/reference/android/graphics/fonts/FontFamily.Builder) A builder class for creating new FontFamily. |\n\n| ### Public methods ||\n|---------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| ` `[Font](/reference/android/graphics/fonts/Font) | ` `[getFont](/reference/android/graphics/fonts/FontFamily#getFont(int))`(int index) ` Returns a font |\n| ` int` | ` `[getSize](/reference/android/graphics/fonts/FontFamily#getSize())`() ` Returns the number of fonts in this FontFamily. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` `[java.lang.Object](/reference/java/lang/Object)` ` |---------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](/reference/java/lang/Object) | ` `[clone](/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object. | | ` boolean` | ` `[equals](/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](/reference/java/lang/Object)` obj) ` Indicates whether some other object is \"equal to\" this one. | | ` void` | ` `[finalize](/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final `[Class](/reference/java/lang/Class)`\u003c?\u003e` | ` `[getClass](/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this `Object`. | | ` int` | ` `[hashCode](/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object. | | ` final void` | ` `[notify](/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` `[notifyAll](/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor. | | ` `[String](/reference/java/lang/String) | ` `[toString](/reference/java/lang/Object#toString())`() ` Returns a string representation of the object. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` `[wait](/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||\n\nPublic methods\n--------------\n\n### getFont\n\nAdded in [API level 29](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic Font getFont (int index)\n```\n\nReturns a font\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------|----------------------------------------------------------|\n| `index` | `int`: an index of the font Value is 0 or greater \u003cbr /\u003e |\n\n| Returns ||\n|------------------------------------------------|-------------------------------------------------------|\n| [Font](/reference/android/graphics/fonts/Font) | a registered font This value cannot be `null`. \u003cbr /\u003e |\n\n### getSize\n\nAdded in [API level 29](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic int getSize ()\n```\n\nReturns the number of fonts in this FontFamily.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-------|-----------------------------------------------------------------------------|\n| `int` | the number of fonts registered in this family. Value is 1 or greater \u003cbr /\u003e |"]]