A Xamarin binding of the Android Calligraphy library from Christopher Jenkins.
Install-Package CallygraphyXamarin
Add your custom fonts to assets/fonts/
all font definitions are relative to this path.
_Note: Be sure to set the build action to AndroidAsset; otherwise Android will default to the theme font.
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.
public override void OnCreate()
{
base.OnCreate();
CalligraphyConfig.InitDefault(new CalligraphyConfig.Builder()
.SetDefaultFontPath("fonts/gtw.ttf")
.SetFontAttrId(Resource.Attribute.fontPath)
// Adding a custom view that support adding a typeFace
// .AddCustomViewWithSetTypeface(Java.Lang.Class.FromType(typeof(CustomViewWithTypefaceSupport)))
// Adding a custom style
// .AddCustomStyle(Java.Lang.Class.FromType(typeof(TextField)), Resource.Attribute.textFieldStyle)
.Build()
);
}
}
Note: You don't need to define CalligraphyConfig
but the library will apply
no default font. I recommend defining at least a default font or attribute.
Wrap the Activity Context:
protected override void AttachBaseContext(Context context)
{
base.AttachBaseContext(CalligraphyContextWrapper.Wrap(context));
}
You're ready to go!
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
<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"/>
<style name="TextViewCustomFont">
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
</style>
<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
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.
#Credits
#Licence
Copyright 2014 Michele Scandura
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.