Font Styles

Along with the built-in fonts, there is a series of styles that are defined as constants in the Typeface class. These styles can be used to modify one of the built-in fonts through the create method available in the Typeface class. This method returns a new Typeface object that can be used.

Here is the list of styles that are defined in the Typeface class:

■ Typeface.BOLD

■ Typeface.ITALIC

■ Typeface.NORMAL

■ Typeface.BOLD_ITALIC

Using one of them is fairly straightforward. First we call Typeface.create, passing in the base font and the style we want to apply. We get back a Typeface that we pass into the Paint.setTypeface method, and that's it.

paint.setColor(Color.GREEN);

paint.setTextSize(40);

Typeface serif_italic = Typeface.create(Typeface.SERIF, Typeface.ITALIC); paint.setTypeface(serif_italic);

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

Figure 4-13. Serif font with Italic style applied

Was this article helpful?

0 0

Post a comment