Convert Figma logo to code with AI

chrisjenx logoCalligraphy

Custom fonts in Android the easy way...

8,588
1,098
8,588
94

Top Related Projects

Bind Android views and callbacks to fields and methods.

2,252

An Android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size.

17,071

An Android library for managing images and the memory they use.

RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android.

Quick Overview

Calligraphy is an Android library that simplifies the process of using custom fonts in Android applications. It allows developers to easily apply custom typefaces to views and text elements throughout their app, without the need to manually set the typeface for each individual view.

Pros

  • Easy integration with minimal setup required
  • Supports applying custom fonts to entire layouts or specific views
  • Works with XML layouts and programmatically created views
  • Offers a solution to the common problem of using custom fonts in Android

Cons

  • No longer actively maintained (last update was in 2017)
  • May not be compatible with the latest Android versions or libraries
  • Limited to font customization and doesn't offer other text styling features
  • Potential performance impact when applying fonts to large layouts

Code Examples

  1. Initializing Calligraphy in your Application class:
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/Roboto-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
    }
}
  1. Applying a custom font to a specific TextView:
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, Calligraphy!"
    fontPath="fonts/Pacifico.ttf" />
  1. Wrapping the base Context in an Activity:
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

Getting Started

  1. Add the Calligraphy dependency to your app's build.gradle:
dependencies {
    implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
}
  1. Initialize Calligraphy in your Application class:
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/Roboto-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
    }
}
  1. Wrap the base Context in your Activities:
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
  1. Use custom fonts in your XML layouts:
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Custom Font Example"
    fontPath="fonts/MyCustomFont.ttf" />

Competitor Comparisons

Bind Android views and callbacks to fields and methods.

Pros of Butterknife

  • More comprehensive view binding solution, handling not just fonts but also resources, listeners, and more
  • Compile-time code generation for better performance and easier debugging
  • Actively maintained with regular updates and improvements

Cons of Butterknife

  • Requires annotation processing, which can slightly increase build times
  • More complex setup compared to Calligraphy's simpler implementation
  • Limited to view binding, while Calligraphy focuses specifically on font management

Code Comparison

Butterknife:

@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@OnClick(R.id.submit) void submit() { ... }

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
}

Calligraphy:

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

// In your layout XML
<TextView
    android:text="@string/hello_world"
    fontPath="fonts/Roboto-Bold.ttf"/>

Butterknife offers a more comprehensive solution for view binding and resource management, while Calligraphy provides a simpler, font-specific implementation. The choice between them depends on project requirements and developer preferences.

2,252

An Android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size.

Pros of sdp

  • Focuses on scalable density pixels for consistent UI across different screen densities
  • Provides a more comprehensive solution for responsive design in Android
  • Actively maintained with recent updates and releases

Cons of sdp

  • Limited to handling only screen size and density variations
  • Requires additional setup and integration compared to Calligraphy
  • May introduce complexity for simpler projects that don't require extensive scaling

Code Comparison

Calligraphy usage:

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
    .setDefaultFontPath("fonts/Roboto-Regular.ttf")
    .setFontAttrId(R.attr.fontPath)
    .build()
);

sdp usage:

<TextView
    android:layout_width="@dimen/_100sdp"
    android:layout_height="@dimen/_50sdp"
    android:textSize="@dimen/_14ssp"/>

While Calligraphy focuses on custom font application, sdp provides a more comprehensive approach to scaling UI elements across different screen sizes and densities. Calligraphy offers a simpler setup for projects primarily concerned with typography, whereas sdp is better suited for applications requiring consistent layouts across various devices. The code examples demonstrate the different approaches: Calligraphy configures fonts programmatically, while sdp utilizes XML attributes for scaling dimensions.

17,071

An Android library for managing images and the memory they use.

Pros of Fresco

  • Comprehensive image loading and caching library, optimized for mobile devices
  • Supports progressive loading and streaming of images
  • Handles complex image manipulations and animations

Cons of Fresco

  • Larger library size and potential overhead for simpler use cases
  • Steeper learning curve due to its extensive feature set
  • May be overkill for projects that only need basic font customization

Code Comparison

Fresco (image loading):

Uri uri = Uri.parse("https://example.com/image.jpg");
SimpleDraweeView draweeView = findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);

Calligraphy (font customization):

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
    .setDefaultFontPath("fonts/Roboto-Regular.ttf")
    .setFontAttrId(R.attr.fontPath)
    .build()
);

Summary

Fresco is a powerful image loading and manipulation library, while Calligraphy focuses on custom font implementation. Fresco offers more features but comes with increased complexity, whereas Calligraphy provides a simpler solution for font customization. Choose based on your project's specific needs and requirements.

RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android.

Pros of richeditor-android

  • Provides a rich text editor with formatting options like bold, italic, underline, etc.
  • Supports image insertion and manipulation within the editor
  • Offers HTML output for easy integration with web content

Cons of richeditor-android

  • More complex to implement and customize compared to Calligraphy
  • Limited to rich text editing functionality, not a general font solution
  • May have a steeper learning curve for developers

Code Comparison

Calligraphy (setting custom font):

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
    .setDefaultFontPath("fonts/Roboto-Regular.ttf")
    .setFontAttrId(R.attr.fontPath)
    .build()
);

richeditor-android (initializing editor):

RichEditor mEditor = (RichEditor) findViewById(R.id.editor);
mEditor.setEditorHeight(200);
mEditor.setEditorFontSize(22);
mEditor.setEditorFontColor(Color.RED);
mEditor.setPadding(10, 10, 10, 10);

Summary

Calligraphy focuses on applying custom fonts throughout an Android app, while richeditor-android provides a rich text editing experience. Calligraphy is simpler to implement for font customization, whereas richeditor-android offers more advanced text editing features but with increased complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

This version of Calligraphy has reached its end-of-life and is no longer maintained. Please migrate to Calligraphy 3!

Calligraphy

Android Arsenal

Custom fonts in Android an OK 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

Include the dependency Download (.aar) :

dependencies {
    compile 'uk.co.chrisjenx:calligraphy:2.3.0'
}

Add Fonts

Add your custom fonts to assets/. All font definitions are relative to this path.

Assuming that you are using Gradle you should create the assets directory under src/main/ in your project directory if it does not already exist. As it's popular to use multi-project build with Gradle the path is usually app/src/main/assets/, where app is the project name.

You might consider creating a fonts/ subdirectory in the assets directory (as in examples).

Usage

<TextView fontPath="fonts/MyFont.ttf"/>

Note: The missing namespace, this IS intentional.

Installation

Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method.

@Override
public void onCreate() {
    super.onCreate();
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()
            );
    //....
}

Note: You don't need to define CalligraphyConfig but the library will apply no default font and use the default attribute of R.attr.fontPath.

Inject into Context

Wrap the Activity Context:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(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"/>

Note: Popular IDE's (Android Studio, IntelliJ) will likely mark this as an error despite being correct. You may want to add tools:ignore="MissingPrefix" to either the View itself or its parent ViewGroup to avoid this. You'll need to add the tools namespace to have access to this "ignore" attribute. xmlns:tools=" http://schemas.android.com/tools". See https://code.google.com/p/android/issues/detail?id=65176.

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 to define a custom attribute.

Why no jar?

We needed to ship a custom ID with Calligraphy to improve the Font Injection flow. This unfortunately means that it has to be an aar. But you're using Gradle now anyway right?

Multiple Typeface's per TextView / Spannables

It is possible to use multiple Typefaces inside a TextView, this isn't new concept to Android.

This could be achieved using something like the following code.

SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append("Hello!") // Bold this
        .append("I use Calligraphy"); // Default TextView font.
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf"));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(sBuilder, TextView.BufferType.SPANNABLE);

Of course this is just an example. Your mileage may vary.

Exceptions / Pitfalls

To our knowledge (try: grep -r -e "void set[^(]*(Typeface " <android source dir>) there are two standard Android widgets that have multiple methods to set typefaces. They are:

  • android.support.v7.widget.SwitchCompat
  • android.widget.Switch

Both have a method called setSwitchTypeface that sets the typeface within the switch (e.g. on/off, yes/no). SetTypeface sets the typeface of the label. You will need to create your own subclass that overrides setTypeface and calls both super.setTypeface and super.setSwitchTypeface.

Collaborators

Note

This library was created because it is currently not possible to declare a custom font in XML files in Android.

If you feel this should be possible to do, please star this issue on the official Android bug tracker.

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.

Badge