When you do a project, you need to use the handwriting font to make the content more beautiful. However, the default application of the system is the default font, how to set the TextView (or edittext) font to the font you want it? The steps are as follows:
1. Download the font file (. ttf format), such as Jinglei.ttf (Founder's font file), and copy it to the project's assets/fonts directory.
2, set the font of TextView:
1 TextView TV = (TextView) Findviewbyid (R.id.my_textview); 2 Typeface Typeface = Typeface.createfromasset (Mcontext.getassets (), "Fonts/jinglei.ttf"); // Mcontext for context 3 tv.settypeface (typeface);
3, in order to be convenient to use, you can also set the operation of the font package into a tool class:
1 /**2 * font-related Operations tool class3 * 4 */5 Public classTypefaceutil {6 //Context7 PrivateContext Mcontext;8 PrivateTypeface Mtypeface;9 Ten /** One * If Ttfpath is null then Mtypeface is the system default value A * - * @paramContext - * @paramTtfpath the */ - - PublicTypefaceutil (Context context, String Ttfpath) { -Mcontext =context; +Mtypeface =Gettypefacefromttf (ttfpath); - } + A /** at * Create typeface object from TTF file - * - * @ttfPath "Fonts/xxx.ttf" - */ - PublicTypeface Gettypefacefromttf (String ttfpath) { - in if(Ttfpath = =NULL) { - returnTypeface.default; to}Else { + returnTypeface.createfromasset (Mcontext.getassets (), ttfpath); - } the } * $ /**Panax Notoginseng * Set the font for TextView - * the * @tv TextView Object + * @ttfPath TTF file path A * @isBold whether to bold font the */ + Public voidSettypeface (TextView TV,Booleanisbold) { - Tv.settypeface (mtypeface); $ setbold (TV, isbold); $ } - - /** the * Set Font bold - */Wuyi Public voidSetbold (TextView TV,Booleanisbold) { theTextpaint TP =tv.getpaint (); - Tp.setfakeboldtext (isbold); Wu } - About /** $ * Set TextView font to system default font - * - */ - Public voidSetdefaulttypeface (TextView TV,Booleanisbold) { A Tv.settypeface (typeface.default); + setbold (TV, isbold); the } - $ /** the * Set the font of the current tool object the * the */ the Public voidsetmtypeface (String ttfpath) { -Mtypeface =Gettypefacefromttf (ttfpath); in } the the}
4, the use of the time just call:
1 New Typefaceutil (Mcontext, "Fonts/jinglei.ttf"); 2 tfutil.settypeface (TV,false);
Android Settings TextView fonts