Awesome
DownloadableCalligraphy
This library provides a way to set default (downloadable) fonts using Calligraphy methods.
This library supports Downloadable Font of Support Library 26.
Credit
This library based on chrisjenx/Calligraphy
And this library use Android Support Library
Getting started
Dependency
implementation 'com.github.takahirom.downloadable.calligraphy:downloadable-calligraphy:[Latest Version]'
Add Fonts
You can use Bundled Font or Downloadable Font.
Downloadable Font
You can add Downloadable Font using Android Studio.
https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html#via-android-studio
Bundled Font
Add your custom fonts to res/font/
. You can see the Font Resource document.
https://developer.android.com/guide/topics/resources/font-resource.html
Usage
<TextView android:fontFamily="@font/my_font"/>
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()
.setDefaultFont(R.font.roboto_regular)
.build()
);
//....
}
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"
android:fontFamily="@font/roboto_bold"/>
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.
View
xml - attr defined here will always take priority.Style
xml - attr defined here is checked next.TextAppearance
xml - attr is checked next, the only caveat to this is IF you have a font defined in theStyle
and aTextAttribute
defined in theView
theStyle
attribute is picked first!Theme
- if defined this is used.Default
- if defined in theCalligraphyConfig
this is used of none of the above are found OR if one of the above returns an invalid font.
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
Licence
Copyright 2013 Christopher Jenkins,
Modifications Copyright (C) 2017 takahirom
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.