Added in API level 1

Typeface


open class Typeface
kotlin.Any
   ↳ android.graphics.Typeface

The Typeface class specifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally Paint settings like textSize, textSkewX, textScaleX to specify how text appears when drawn (and measured).

Summary

Nested classes

A builder class for creating new Typeface instance.

A builder class for creating new Typeface instance.

Constants
static Int

static Int

static Int

static Int

Public methods
open static Typeface!
create(family: Typeface!, style: Int)

Create a typeface object that best matches the specified existing typeface and the specified Style.

open static Typeface
create(family: Typeface?, weight: Int, italic: Boolean)

Creates a typeface object that best matches the specified existing typeface and the specified weight and italic style

open static Typeface!
create(familyName: String!, style: Int)

Create a typeface object given a family name, and option style information.

open static Typeface!

Create a new typeface from the specified font data.

open static Typeface!

Create a new typeface from the specified font file.

open static Typeface!

Create a new typeface from the specified font file.

open static Typeface!

Returns one of the default typeface objects, based on the specified style

open Boolean
equals(other: Any?)

Indicates whether some other object is "equal to" this one.

open Int

Returns the typeface's intrinsic style attributes

String?

Returns the system font family name if the typeface was created from a system font family, otherwise returns null.

open Int

Returns the typeface's weight value

open Int

Returns a hash code value for the object.

Boolean

Returns true if getStyle() has the BOLD bit set.

Boolean

Returns true if getStyle() has the ITALIC bit set.

Properties
static Typeface!

The default NORMAL typeface object

static Typeface!

The default BOLD typeface object.

static Typeface!

The NORMAL style of the default monospace typeface.

static Typeface!

The NORMAL style of the default sans serif typeface.

static Typeface!

The NORMAL style of the default serif typeface.

Constants

BOLD

Added in API level 1
static val BOLD: Int
Value: 1

BOLD_ITALIC

Added in API level 1
static val BOLD_ITALIC: Int
Value: 3

ITALIC

Added in API level 1
static val ITALIC: Int
Value: 2

NORMAL

Added in API level 1
static val NORMAL: Int
Value: 0

Public methods

create

Added in API level 1
open static fun create(
    family: Typeface!,
    style: Int
): Typeface!

Create a typeface object that best matches the specified existing typeface and the specified Style. Use this call if you want to pick a new style from the same family of an existing typeface object. If family is null, this selects from the default font's family.

This method is not thread safe on API 27 or before. This method is thread safe on API 28 or after.

Parameters
family Typeface!: An existing Typeface object. In case of null, the default typeface is used instead.
style Int: The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC.
Value is one of the following:
Return
Typeface! The best matching typeface.

create

Added in API level 28
open static fun create(
    family: Typeface?,
    weight: Int,
    italic: Boolean
): Typeface

Creates a typeface object that best matches the specified existing typeface and the specified weight and italic style

Below are numerical values and corresponding common weight names.

Value Common weight name
100 Thin
200 Extra Light
300 Light
400 Normal
500 Medium
600 Semi Bold
700 Bold
800 Extra Bold
900 Black

This method is thread safe.

Parameters
family Typeface?: An existing Typeface object. In case of null, the default typeface is used instead.
weight Int: The desired weight to be drawn.
Value is between 1 and 1000 inclusive
italic Boolean: true if italic style is desired to be drawn. Otherwise, false
Return
Typeface A Typeface object for drawing specified weight and italic style. Never returns null

create

Added in API level 1
open static fun create(
    familyName: String!,
    style: Int
): Typeface!

Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

Parameters
familyName String!: May be null. The name of the font family.
style Int: The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC.
Value is one of the following:
Return
Typeface! The best matching typeface.

createFromAsset

Added in API level 1
open static fun createFromAsset(
    mgr: AssetManager!,
    path: String!
): Typeface!

Create a new typeface from the specified font data.

Parameters
mgr AssetManager!: The application's asset manager
path String!: The file name of the font data in the assets directory
Return
Typeface! The new typeface.

createFromFile

Added in API level 4
open static fun createFromFile(file: File?): Typeface!

Create a new typeface from the specified font file.

Parameters
file File?: The path to the font data.
This value may be null.
Return
Typeface! The new typeface.

createFromFile

Added in API level 4
open static fun createFromFile(path: String?): Typeface!

Create a new typeface from the specified font file.

Parameters
path String?: The full path to the font data.
This value may be null.
Return
Typeface! The new typeface.

defaultFromStyle

Added in API level 1
open static fun defaultFromStyle(style: Int): Typeface!

Returns one of the default typeface objects, based on the specified style

Parameters
style Int: Value is one of the following:
Return
Typeface! the default typeface that corresponds to the style

equals

Added in API level 1
open fun equals(other: Any?): Boolean

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj the reference object with which to compare.
Return
Boolean true if this object is the same as the obj argument; false otherwise.

getStyle

Added in API level 1
open fun getStyle(): Int

Returns the typeface's intrinsic style attributes

Return
Int Value is one of the following:

getSystemFontFamilyName

Added in API level 34
fun getSystemFontFamilyName(): String?

Returns the system font family name if the typeface was created from a system font family, otherwise returns null.

getWeight

Added in API level 28
open fun getWeight(): Int

Returns the typeface's weight value

Return
Int Value is between 0 and 1000 inclusive

hashCode

Added in API level 1
open fun hashCode(): Int

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Return
Int a hash code value for this object.

isBold

Added in API level 1
fun isBold(): Boolean

Returns true if getStyle() has the BOLD bit set.

isItalic

Added in API level 1
fun isItalic(): Boolean

Returns true if getStyle() has the ITALIC bit set.

Properties

DEFAULT

Added in API level 1
static val DEFAULT: Typeface!

The default NORMAL typeface object

DEFAULT_BOLD

Added in API level 1
static val DEFAULT_BOLD: Typeface!

The default BOLD typeface object. Note: this may be not actually be bold, depending on what fonts are installed. Call getStyle() to know for sure.

MONOSPACE

Added in API level 1
static val MONOSPACE: Typeface!

The NORMAL style of the default monospace typeface.

SANS_SERIF

Added in API level 1
static val SANS_SERIF: Typeface!

The NORMAL style of the default sans serif typeface.

SERIF

Added in API level 1
static val SERIF: Typeface!

The NORMAL style of the default serif typeface.