Stay organized with collections
Save and categorize content based on your preferences.
FontFamily
class FontFamily
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 |
|
A builder class for creating new FontFamily.
|
Public methods |
Font |
Returns a font
|
Int |
Returns the number of fonts in this FontFamily.
|
Public methods
getFont
fun getFont(index: Int): Font
Returns a font
Parameters |
index |
Int: an index of the font Value is 0 or greater |
Return |
Font |
a registered font This value cannot be null . |
getSize
fun getSize(): Int
Returns the number of fonts in this FontFamily.
Return |
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# FontFamily\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nFontFamily\n==========\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/graphics/fonts/FontFamily \"View this page in Java\") \n\n```\nclass FontFamily\n```\n\n|---|----------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.graphics.fonts.FontFamily](#) |\n\nA font family class can be used for creating Typeface.\n\nA 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. \n\n```kotlin\nFontFamily 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\nSummary\n-------\n\n| Nested classes ||\n|---|---------------------------------------------------------------------------------------------------------------------|\n| | [Builder](/reference/kotlin/android/graphics/fonts/FontFamily.Builder) A builder class for creating new FontFamily. |\n\n| Public methods ||\n|----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| [Font](/reference/kotlin/android/graphics/fonts/Font) | [getFont](#getFont(kotlin.Int))`(`index:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Returns a font |\n| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [getSize](#getSize())`()` Returns the number of fonts in this FontFamily. |\n\nPublic methods\n--------------\n\n### getFont\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun getFont(index: Int): Font\n```\n\nReturns a font\n\n| Parameters ||\n|---------|------------------------------------------------------------------------------------------------------------------------|\n| `index` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): an index of the font Value is 0 or greater |\n\n| Return ||\n|-------------------------------------------------------|------------------------------------------------|\n| [Font](/reference/kotlin/android/graphics/fonts/Font) | a registered font This value cannot be `null`. |\n\n### getSize\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun getSize(): Int\n```\n\nReturns the number of fonts in this FontFamily.\n\n| Return ||\n|----------------------------------------------------------------------------|----------------------------------------------------------------------|\n| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | the number of fonts registered in this family. Value is 1 or greater |"]]