Skip to content

hkiehs/Calligraphy

 
 

Repository files navigation

Calligraphy

Custom fonts in Android the easy way.

Are you fed up of Custom views to set fonts? Or traversing the ViewTree to find TextViews? Yeah me too.

alt text

##Getting started

Dependency

Download from Maven Central (.jar)

OR

Include the dependency:

dependencies {
    compile 'uk.co.chrisjenx:calligraphy:1.0.+'
}

Fonts

Add your custom fonts to assets/ all font definition is relative to this path.

Custom Attribute

We don't package an R.attr with Calligraphy to keep it a Jar. So you will need to add your own Attr.

The most common one is: res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="fontPath" format="string"/>
</resources>

Configuration

Define your default font using CalligraphyConfig, in your Application class, unfortunately Activity#onCreate(Bundle) is called after Activity#attachBaseContext(Context) so the config needs to be defined before that.

protected void onCreate() {
    super.onCreate();
    CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);
    //....
}

Note: You don't need to define CalligraphyConfig anymore (1.0.0+) but the library will apply no default font. I recommend defining at least a default font or attribute.

Inject into Context

Wrap the Activity Context:

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

You're good to go!

Usage

Custom font per TextView

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

Custom font in TextAppearance

<style name="TextAppearance.FontPath" parent="android:TextAppearance">
    <!-- Custom Attr-->
    <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>
<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance_FontPath/>

Custom font in Styles

<style name="TextViewCustomFont">
    <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>

Custom font defined in Theme

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
</style>

<style name="AppTheme.Widget"/>

<style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView">
    <item name="fontPath">fonts/Roboto-ThinItalic.ttf</item>
</style>

#FAQ

Font Resolution

The CalligraphyFactory looks for the font in a pretty specific order, for the most part it's very similar to how the Android framework resolves attributes.

  1. View xml - attr defined here will always take priority.
  2. Style xml - attr defined here is checked next.
  3. TextAppearance xml - attr is checked next, the only caveat to this is IF you have a font defined in the Style and a TextAttribute defined in the View the Style attribute is picked first!
  4. Theme - if defined this is used.
  5. Default - if defined in the CalligraphyConfig this is used of none of the above are found OR if one of the above returns an invalid font.

Why not piggyback off of fontFamily attribute?

We originally did, but it conflicted with users wanting to actually use that attribute, you now have have to define a custom attribute.

Why not ship with custom attribute?

No resources means that the library can compile down to a jar instead of an aar, as I know alot of users are still not using Gradle yet.

As of 1.0+ you have to define a custom attribute.

#Colaborators

#Licence

Copyright 2013 Christopher Jenkins

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Custom fonts in Android the easy way...

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%