Android Settings TextView fonts

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.