Giter Site home page Giter Site logo

calligraphy's Introduction

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

calligraphy's People

Contributors

0mega avatar barbosa avatar barnhill avatar bod avatar bryant1410 avatar chrisjenx avatar codebutler avatar cs-victor-nascimento avatar dlew avatar ened avatar fingertricks avatar hesamedin avatar libtastic avatar loeschg avatar loganj avatar manish05 avatar manuelpeinado avatar matthewmichihara avatar mluedke2 avatar nsk-mironov avatar ryanmeador avatar scottyab avatar shaunidiot avatar smuldr avatar taxomania avatar ukeller avatar veinhorn avatar wasabeef avatar wieczorek1990 avatar zacsweers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

calligraphy's Issues

Null cache invalid fonts

To stop the AssetManger trying to load invalid fonts multiple times, cache the font path as null and always return null thereafter.

Custom Fonts not loaded, default font loaded

I have defined the default font which loads fine:

        CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);

and also:

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

This works fine. The Roboto-Regular font is then applied.

I am then trying to set a custom font to a TextView, however it doesn't get loaded:

<TextView
   android:id="@+id/titleDrawer"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   fontPath="fonts/Roboto-LightItalic.ttf"
   android:textColor="@color/white"
   android:textSize="18sp"
 />

No build issues or comments in the log.

What's strange is, I looked at my app before and the custom font was loaded. I didn't change any code, went to rebuild my app and now the custom fonts have gone.
Not really sure what's happening here!

Move initialize to Application class

I think better to move all initialization of library to Application class. You can override

public class CoreApplication extends Application {

   @Override
   public void onCreate() {
        //todo init fonts
   }

    @Override
    public Object getSystemService(String name) {
        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            if (mInflater == null) {
                mInflater = new CalligraphyLayoutInflater(LayoutInflater.from(getBaseContext()), this);
            }
            return mInflater;
        }
        return super.getSystemService(name);
    }
}

in this case all initialization will be in one place for all application and we don't need to add some specific logic inside activity.

I have not test this solution but I think it will be work.

Regards,
Vova.

Issue with mixing standard fonts and caligraphy fonts

I setup default font to "fonts/somefont.ttf"
However In some TextViews I would like to use roboto fonts so I define text view :

<TextView android:id="@+id/test" android:fontFamily="sans-serif-light" ...
http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to

this results in debug warnings :
"Can't create asset from sans-serif-light. Make sure you have passed in the correct path and file name."

is this a bug? or maybe there is some other way to add roboto.

P.S - it seems like this guy had a similar issue .. "sans-serif-light" was within library he references #32

1.1.0

Primarily bug fixes.
Or missed features for 1.0.0.

Spannable doesn't display properly after 1.0.0 update

I updated to 1.0.0 today, and certain textviews that I set with spannables (custom fontface) are no longer appearing correctly (font-awesome). The characters display as boxes.

This is the custom spannable:

/**
 * Style a {@link Spannable} with a custom {@link Typeface}.
 *
 * @author Tristan Waddington
 */
public class TypefaceSpan extends MetricAffectingSpan {
    private Typeface mTypeface;

    /**
     * Load the {@link Typeface} and apply to a {@link Spannable}.
     */
    public WWTypefaceSpan(AssetManager assets, String typefaceName) {
        mTypeface = TypefaceUtils.load(assets, typefaceName);
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

Items in navigation drawer don't have the custom font?

Hi,

This library works great but I have one problem, the navigation drawer items don't get the custom font I apply to them in xml.

<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    fontPath="fonts/AvenirNextCondensed_Italic.ttf"
    tools:text="Nieuw" />

This is for example a navigation item xml layout.
In my activity with the navigation drawer I add

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

It does work in every other part of my app. Is this a known bug or am I doing something wrong ?

Support default font for bold/italic style?

I don't know if this feature has been implemented or not. I just recently applying OpenSans-Regular.ttf as global default, then all bold TextView turned to normal (non-bold style). I wish there is a built-in and clean way to provide default bold font file (e.g. OpenSans-Bold.ttf) when used by TextView, perhaps by checking it in TextView#getTypeface().isBold().

I think this feature will come in handy because it will eliminate the need to apply custom style one by one to all bold/italic texts.

Thanks

Warning when using it with betterpickers library

I'm getting the following warning on a project, just wondering if anyone knows why and how to get rid of it, my project works fine :)

The warning fires every time i open a dialog created with betterpickers https://github.com/derekbrameyer/android-betterpickers

04-21 16:10:27.356: W/Calligraphy(1496): Can't create asset from sans-serif-light. Make sure you have passed in the correct path and file name.
04-21 16:10:27.356: W/Calligraphy(1496): java.lang.RuntimeException: native typeface cannot be made
04-21 16:10:27.356: W/Calligraphy(1496): at android.graphics.Typeface.(Typeface.java:147)
04-21 16:10:27.356: W/Calligraphy(1496): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.TypefaceUtils.load(TypefaceUtils.java:33)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyUtils.applyFontToTextView(CalligraphyUtils.java:30)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyUtils.applyFontToTextView(CalligraphyUtils.java:42)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreated(CalligraphyFactory.java:116)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onCreateView(CalligraphyFactory.java:65)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-21 16:10:27.356: W/Calligraphy(1496): at com.doomonafireball.betterpickers.numberpicker.NumberPicker.(NumberPicker.java:75)
04-21 16:10:27.356: W/Calligraphy(1496): at java.lang.reflect.Constructor.constructNative(Native Method)
04-21 16:10:27.356: W/Calligraphy(1496): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyFactory.createViewOrFailQuietly(CalligraphyFactory.java:89)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyFactory.createViewOrFailQuietly(CalligraphyFactory.java:73)
04-21 16:10:27.356: W/Calligraphy(1496): at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onCreateView(CalligraphyFactory.java:61)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-21 16:10:27.356: W/Calligraphy(1496): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-21 16:10:27.356: W/Calligraphy(1496): at com.doomonafireball.betterpickers.numberpicker.NumberPickerDialogFragment.onCreateView(NumberPickerDialogFragment.java:143)

Is there a way to easily make it attach to custom views too?

Hi

Thanks for the library!

Is there a way to make Calligaph apply to custom views too?

I don't know if it is something about where TextPaint gets it's typeface from, but it would be great if it could be extended to automatically adjust.

Best Regards

Andreas

Crash on LG devices after click on ActionBar

This bug is very bizarre and reproduced only on LG devices on android 4.1.2 when text of ActionBar or ActionBar menu items contains spans. As I know Calligraphy adds CalligraphyTypefaceSpan to set custom font after update to 1.1.0. Stacktarce is:

java.lang.IllegalArgumentException: Invalid payload item type
       at android.util.EventLog.writeEvent(EventLog.java)
       at android.app.Activity.onMenuItemSelected(Activity.java:2757)
       at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:172)
       at android.view.View.performClick(View.java:4114)
       at android.view.View$PerformClick.run(View.java:17095)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4921)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
       at dalvik.system.NativeStart.main(NativeStart.java)

Similar issues: 1, 2, 3

"android:fontFamily" attribute is ignored if specified via the "android:textAppearance" in style

The "android:fontFamily" attribute works perfectly if specified directly on the TextView style, but is ignored if set via the "android:textAppearance":

<style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:textAppearance">@style/TextAppearanceStyle</item>
</style>

<style name="TextAppearanceStyle" parent="android:TextAppearance" >
    <item name="android:fontFamily">fonts/SOME-FONT-HERE</item>
</style>

It would be great to support all TextView styling capabilities including via the "android:textAppearance".

Doesn't seem to work with Spinner widget?

android:fontfamily is ignored in Spinner widget and spinner is using default font set by CalligraphyConfig.initDefault() in the activity. Any idea what seems to be the problem?

Thanks

android:maxLines and android:ellipsis results in ellipsis not displayed

A TextView defining both maxLine (whatever the value) and ellipsize results in the TextView applying the custom font but not displaying the ellipsis (...) for strings longer than maxLines.

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fontPath="@string/media_item_title_font"      
        android:maxLines="2"
        android:ellipsize="end"
        tools:ignore="MissingPrefix" />

With the following text:

this is line1
this is line2
this is line3

the TextView above will display:

this is line1
this is line2

instead of:

this is line1
this is line2...

Removing the fontPath element produces the expected result

Setting font from Java code

Hey guys, Nice library! It's really useful and helpful.

I have one issue I am unable to resolve using this. I have a Class which extends TextView. I want to set a font for all instances of this class from the constructor. I have tried to do this, but the Apps default font always overwrites this. Is this a bug in the Library? or am I missing how to do this?

I have tried to set the TextView's Typeface, I have tried using the utilities supplied by Calligraphy. I have tried use a default custom style.

Any help would be much appreciated.

Custom font attribute required in Gingerbread

Using Calligraphy without the custom font attribute results in a Resources.NotFoundException when inflating a layout. When a custom font attribute is passed to the CalligraphyContext, the issue is solved.

Full stack trace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calligraphytest.app/com.example.calligraphytest.app.MainActivity}: android.view.InflateException: Binary XML file line #29: Error inflating class TextView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            (...)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class TextView
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
            at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2206)
            (...)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ย ย ย ย ย ย ย ย ย ย ย ย at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x10103ac
            at android.content.res.Resources.getResourceEntryName(Resources.java:1499)
            at uk.co.chrisjenx.calligraphy.CalligraphyUtils.pullFontPath(CalligraphyUtils.java:49)
            at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onViewCreated(CalligraphyFactory.java:101)
            at uk.co.chrisjenx.calligraphy.CalligraphyFactory.onCreateView(CalligraphyFactory.java:65)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
ย ย ย ย ย ย ย ย ย ย ย ย at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
ย ย ย ย ย ย ย ย ย ย ย ย at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
ย ย ย ย ย ย ย ย ย ย ย ย at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
ย ย ย ย ย ย ย ย ย ย ย ย at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
ย ย ย ย ย ย ย ย ย ย ย ย at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2206)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2261)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:203)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.Activity.setContentView(Activity.java:1657)
ย ย ย ย ย ย ย ย ย ย ย ย at com.example.calligraphytest.app.MainActivity.onCreate(MainActivity.java:17)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.ActivityThread.access$1500(ActivityThread.java:117)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
ย ย ย ย ย ย ย ย ย ย ย ย at android.os.Handler.dispatchMessage(Handler.java:99)
ย ย ย ย ย ย ย ย ย ย ย ย at android.os.Looper.loop(Looper.java:130)
ย ย ย ย ย ย ย ย ย ย ย ย at android.app.ActivityThread.main(ActivityThread.java:3683)
ย ย ย ย ย ย ย ย ย ย ย ย at java.lang.reflect.Method.invokeNative(Native Method)
ย ย ย ย ย ย ย ย ย ย ย ย at java.lang.reflect.Method.invoke(Method.java:507)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ย ย ย ย ย ย ย ย ย ย ย ย at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ย ย ย ย ย ย ย ย ย ย ย ย at dalvik.system.NativeStart.main(Native Method)

I am using a blank "Hello, World" project:

apply plugin: 'android'

android {
  compileSdkVersion 19
  buildToolsVersion "19.0.3"

  defaultConfig {
    minSdkVersion 10
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
  }
}    

dependencies {
  compile "uk.co.chrisjenx:calligraphy:0.7.+"
}
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CalligraphyConfig.initDefault("UbuntuMono-R.ttf");
        setContentView(R.layout.activity_main);
    }

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

IndexOutOfBoundsException in 4.0.4 platform with actionbar

Hi, thanks for this wonderful library.
I use this library in one of my application, everything is right till I try to run a suitability test online, and I got the crash message for particular model, including Note 1, S II, both android 4.0.4 ( I could provide full list if needed).

there is the logcat info

05-26 11:01:24.940E16485AndroidRuntimejava.lang.RuntimeException: Unable to start activity ComponentInfo{com.ailk.insight.dev/com.ailk.insight.activity.Main}: android.view.InflateException: Binary XML file line #38: Error inflating class
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread.access$600(ActivityThread.java:127)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-26 11:01:24.940E16485AndroidRuntimeat android.os.Handler.dispatchMessage(Handler.java:99)
05-26 11:01:24.940E16485AndroidRuntimeat android.os.Looper.loop(Looper.java:137)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread.main(ActivityThread.java:4511)
05-26 11:01:24.940E16485AndroidRuntimeat java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:01:24.940E16485AndroidRuntimeat java.lang.reflect.Method.invoke(Method.java:511)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
05-26 11:01:24.940E16485AndroidRuntimeat dalvik.system.NativeStart.main(Native Method)
05-26 11:01:24.940E16485AndroidRuntimeCaused by: android.view.InflateException: Binary XML file line #38: Error inflating class
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2885)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2945)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1686)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.Activity.initActionBar(Activity.java:1816)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.Activity.getActionBar(Activity.java:1803)
05-26 11:01:24.940E16485AndroidRuntimeat com.actionbarsherlock.internal.ActionBarSherlockNative.initActionBar(ActionBarSherlockNative.java:40)
05-26 11:01:24.940E16485AndroidRuntimeat com.actionbarsherlock.internal.ActionBarSherlockNative.getActionBar(ActionBarSherlockNative.java:35)
05-26 11:01:24.940E16485AndroidRuntimeat com.actionbarsherlock.app.SherlockFragmentActivity.getSupportActionBar(SherlockFragmentActivity.java:42)
05-26 11:01:24.940E16485AndroidRuntimeat com.ailk.insight.activity.Main.onCreate(Main.java:82)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.Activity.performCreate(Activity.java:4470)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
05-26 11:01:24.940E16485AndroidRuntimeat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
05-26 11:01:24.940E16485AndroidRuntime... 11 more
05-26 11:01:24.940E16485AndroidRuntimeCaused by: java.lang.reflect.InvocationTargetException
05-26 11:01:24.940E16485AndroidRuntimeat java.lang.reflect.Constructor.constructNative(Native Method)
05-26 11:01:24.940E16485AndroidRuntimeat java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-26 11:01:24.940E16485AndroidRuntime... 30 more
05-26 11:01:24.940E16485AndroidRuntimeCaused by: android.view.InflateException: Binary XML file line #36: Error inflating class TextView
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-26 11:01:24.940E16485AndroidRuntimeat android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.widget.ActionBarView.initTitle(ActionBarView.java:826)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.widget.ActionBarView.setDisplayOptions(ActionBarView.java:623)
05-26 11:01:24.940E16485AndroidRuntimeat com.android.internal.widget.ActionBarView.(ActionBarView.java:254)
05-26 11:01:24.940E16485AndroidRuntime... 33 more
05-26 11:01:24.940E16485AndroidRuntimeCaused by: java.lang.IndexOutOfBoundsException
05-26 11:01:24.940E16485AndroidRuntimeat android.content.res.StringBlock.nativeGetString(Native Method)
05-26 11:01:24.940E16485AndroidRuntimeat android.content.res.StringBlock.get(StringBlock.java:81)
05-26 11:01:24.940E16485AndroidRuntimeat android.content.res.AssetManager.getPooledString(AssetManager.java:274)
05-26 11:01:24.940E16485AndroidRuntimeat android.content.res.TypedArray.loadStringValueAt(TypedArray.java:726)
05-26 11:01:24.940E16485AndroidRuntimeat android.content.res.TypedArray.getString(TypedArray.java:124)
05-26 11:01:24.940E16485AndroidRuntimeat uk.co.chrisjenx.calligraphy.CalligraphyUtils.pullFontPathFromStyle(CalligraphyUtil

And If I remove Calligraphy from my app, everything goes right.

TextAllCaps doesn't work in theme

Hi,

I'm trying to set the font of my actionbar title in the theme.
This is my xml style:

<style name="HomeActivityTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/HomeActivityTheme.ActionBarStyle</item>
    <item name="android:actionBarWidgetTheme">@style/HomeActivityTheme.ActionBarWidgetTheme</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:actionBarItemBackground">@drawable/home_theme_actionbar_item_bg</item>
    <item name="android:windowBackground">@drawable/cotton_bg</item>
</style>

<style name="HomeActivityTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:titleTextStyle">@style/ActionBar.WhiteTextStyle</item>
    <item name="android:background">@color/immozo_blue</item>
</style>

<style name="ActionBar.WhiteTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@android:color/white</item>
    <item name="fontPath">fonts/AvenirNextCondensed_Regular.ttf</item>
    <item name="android:textSize">@dimen/medium_fontsize</item>
    <item name="android:textAllCaps">true</item>
</style>

I apply this theme in the androidManifest.

All the attributes work, but textAllCaps has no effect. I've read that in version 1.1.0 there were some fixes to a textAllCaps bug but I have 1.1.0.

TextView Font renders only onResume

Here is something strange that happens on my device.

I have set a global theme in TextView e.g. Roboto-Medium (overriding the theme's "android:textViewStyle") and the rest inside the Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CalligraphyConfig.initDefault("fonts/Roboto-Medium.ttf", R.attr.fontPath);
}


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

For a particular TextView i want another font so I set a custom textAppearance:

  <TextView
  android:textAppearance="@style/AppTheme.TextAppearance.Reditum"
  ... />

Strangely when I open the activity the font has not rendered but when I resume (I guess something happens onResume that did not before), it is correctly shown.

Is that reproducible?

Modify ActionBar Font (suggested feature)

I'm currently developing an app and I'm using Calligraphy almost anywhere in it to display Roboto-Light. The only thing that I can't seem to find is using Calligraphy to modify the action bar's title font: Even if I declare fontPath = "..." inside the action bar's style, it seems to be ignored.

This is more of a suggestion to add this as a feature rather than an issue.

1.0.0 doesn't work for the ActionBar's android:titleTextStyle

This used to work with 0.7.2:

<!-- Theme -->
<item name="android:actionBarStyle">@style/my_action_bar</item>

<!-- my_action_bar -->
<item name="android:titleTextStyle">@style/my_title_style</item>

<!-- my_title_style -->
<item name="fontPath">fonts/Lato-Black.ttf</item>

textStyle or fontFamily in XML is not applied

I have set custom font from assets via Application class and overridden the Activity base context callback. Now I want to customize a little one TextView. I have set different fontFamily and textStyle (bold). The TextView is not displayed correctly (textStyle and fontFamily is not applied). I'm using 0.7.2. I've done everything exactly what described in the README. What can be wrong?

Distribute as AAR as well

It'd be useful to distribute this as an AAR as well as a jar, for those of us who have switched to gradle.

unexpected namespace prefix "app" found for tag TextView

IDE: Android Studio 0.8.2

Whenever I use the app:fontPath in a layout in Android Studio, I get a warning "unexpected namespace prefix "app" found for tag TextView." I'm adding xmlns:app="http://schemas.android.com/apk/res-auto" in my root layout. I have to add tools:ignore="MissingPrefix" to the view to remove the warning.

screenshot 2014-07-16 11 16 17

I have the following in my values/attr.xml:

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

Edit: To be clear, this does not break anything. It's just a slight irritant ๐Ÿ˜€

Investigate custom font attribute

As Hugo Visser pointed out, using fontFamily might make lint whine a bit, and also stops people using fontFamily in a valid fashion.

Add a custom attribute for an 'aar' build.

CheckBox

Calligraphy doesn't seem to set the font for the TextViews in CheckBoxes. Not sure if this is related but setting the android:fontFamily also doesn't seem to work either. Only way I've gotten it to work was in code with .setTypeFace(Typeface)

Support for the new Android Toolbar component?

Hi, I'm playing around with the Android L Preview, and I've noticed that when I use the toolbar.setTitleTextAppearance() method to apply a style that has a Calligraphy fontPath supplied, everything in that style except the custom font is applied. Is this a known bug?

setTypeface() does not work on CustomTextView

setTypeface() does not work on the CustomTextView (subclass of TextView).

MainActivity.java

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ButterKnife.inject(this, view);

            Typeface robotoBold = Typeface.createFromAsset(getResources().getAssets(), "fonts/Roboto-Bold.ttf");

            TextView customTextView = ButterKnife.findById(view, R.id.typeface_roboto_bold_custom);
            customTextView.setTypeface(robotoBold);

            TextView textView = ButterKnife.findById(view, R.id.typeface_roboto_bold);
            textView.setTypeface(robotoBold);
        }

fragment_main.xml

        <uk.co.chrisjenx.calligraphy.sample.CustomTextView
            android:id="@+id/typeface_roboto_bold_custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This has a typeface set to Roboto Bold, on the custom TextView.\n"/>

        <TextView
            android:id="@+id/typeface_roboto_bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This has a typeface set to Roboto Bold, on the View.\n"/>

Result

calligraphycustomtextview

Possible Memory Leak on Calligraphy 0.7.x?

I'm still using the 0.7.x version of your library, but maybe the problem (if it is a real problem) still holds for the current version.

Currently, I'm investigating memory footprint and usage of my app. Via Android Studio, I can record the memory usage of my app with a pretty easy overview. This overview, among other stuff, also displays the allocation of assets. I load two custom fonts for my app using Calligraphy. When I start my app, these two fonts have two references each - so it allocates each font twice. After surfing around, I can see that one of the fonts is allocated 6 times in memory (while the other font keeps with 2).

Asset Allocations
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Book.otf: 133K
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***.apk:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***:/assets/fonts/GothamRnd-Medium.otf: 123K
    zip:/data/app/***:/assets/fonts/GothamRnd-Book.otf: 133K

What's going on there? I'm setting the font via styles in XML. But I'm not sure whether the memory leak is my fault (which I assume), or whether it's maybe a calligraphy bug.

Add information in the README file

I think it would make sense to add a link to this issue:
https://code.google.com/p/android/issues/detail?id=1087
in the README file so people know why this library is needed in the first place, and also entice them to "star" it so maybe one day it will be resolved :)

Don't get me wrong, this lib is really cool and I thank you for creating it, but it would be a lot preferable to have the feature directly in the platform.

Remove required default font.

It would be best to remove the need to require a default font.
As this breaks fontFamily attribute in some setups.

I am getting NoClassDefFoundError.

First, I download jar and import this.
Second, onCreate in Application class write code about CalligraphyConfig init.
Last, attachBaseContext in Activity write code about CalligraphyContextWrapper.

But I get some error.... I don't know why.
Please help me.

Using Calligraphy without modifing Toasts

Hello,
I'm using Calligraphy in an app I'm developing and its a great solution to the fonts issues in android. The only thing I can't seem to figure out is how to prevent Calligraphy from modifying toasts - it seems that the line CalligraphyConfig.initDefault("fonts/Roboto-Thin.ttf"); changes the Toasts' fonts as well, is there any way around this? not calling initDefault causes a crash.

Fragment issue

Hi, great library!
There's this issue where the calligraphy won't work. I have a nav drawer with a main activity(which includes the calligraphy) and 3 fragments attached to it for navigation.
The simple fragment will display my custom font correctly but the other two, which have listviews, won't display the font.
Any ideas?

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.