Giter Site home page Giter Site logo

fontutil's Introduction

Update

The Support Liberary now supporting font resources. Please check the link below : https://youtu.be/V6-roIeNUY0?t=7m17s

FontUtil

a simple way to change the typeface allover your app or to a specific view directly from the layout xml without creating a custom class or refering to your view programmatically.

How to use it

  • add the below dependency to your gradle file
compile 'com.khouli.font:font-util:0.0.1'
  • if you want to change the typeface in all the text views (TextView or any of its subclasses) all over the application apply font as below
public class FontApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        
        FontUtil.applyFont(Typeface.createFromAsset(getAssets(),"fonts/Pacifico.ttf"));
    }
    
    //dont forget to change the app name in your manifiest android:name=".your custom class name"
}
  • OR if you want to change the typeface in any specific text view (TextView or any of its subclasses) directly from the layout xml init the library as below
public class FontApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        //hash map of (font name , font path in assets)
        HashMap<String,String> fontsMap = new HashMap<>();
        fontsMap.put(this.getResources().getString(R.string.pacifico_font),"fonts/Pacifico.ttf");

        FontUtil.init(this,fontsMap);
    }
}

and now to use the font in any layout just set the font name to the tag object as below

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="@string/pacifico_font" 
        ...
        />
  • last step is to override your attachBaseContext method in your base activity as below
public class YourBaseActivity extends AppCompatActivity {

   @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(new UtilContextWrapper(newBase));

    }
    ... 
    }

please check the sample app to see it in action.

Contribution

if you find a bug ? or you have feature request? Please send a pull request or add an issue in the tracker.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.