BuiltIn Fonts

Drawing text without being able to specify a font or style would be pretty limiting. Fortunately, the Paint class allows us to specify which font should be used by calling the setTypeface method and passing in a Typeface object.

The Typeface class has a number of constants defined that represent the built-in fonts that come with the Android OS. These fonts were created by a company called Ascender (www.ascendercorp.com/) as part of their Droid suite of fonts.

They are defined in the Typeface class as follows:

Typeface.MONOSPACE: This font has equal spacing for each letter.

Typeface.SANS_SERIF: This is a font that doesn't have serifs.

Typeface.SERIF: This is a font that contains serifs.

NOTE: Serifs are small lines at the ends of the lines that make up the letters. The font that you are reading right now is a sansserif font. This is an example of a serif font.

Hello

Figure 4-9. Typeface.MONOSPACE example

Hello

Figure 4-10. Typeface.SANS_SERIF example

Hello

Figure 4-11. Typeface.SERIF example

In addition to the main three fonts, there are two other Typeface constants:

■ Typeface.DEFAULT: This is the same as the sanserif font and is the default font that is used if setTypeface is not called.

■ Typeface.DEFAULT_BOLD: This is a bold version of the sanserif font. Here is a short code example:

paint.setColor(Color.GREEN);

paint.setTextSize(40);

paint.setTypeface(Typeface.DEFAULT_BOLD);

canvas.drawText("Hello", text_x, text_y, paint);

Figure 4-12. Typeface.DEFAULT_BOLD

Was this article helpful?

0 0

Post a comment