https://github.com/nwestfall/calligraphy.xamarin
Use custom fonts in Android! Port of https://github.com/InflationX/Calligraphy
https://github.com/nwestfall/calligraphy.xamarin
android calligraphy font typeface xamarin xamarin-android xamarin-android-library
Last synced: about 1 month ago
JSON representation
Use custom fonts in Android! Port of https://github.com/InflationX/Calligraphy
- Host: GitHub
- URL: https://github.com/nwestfall/calligraphy.xamarin
- Owner: nwestfall
- License: apache-2.0
- Created: 2018-05-23T03:36:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-26T17:45:27.000Z (over 4 years ago)
- Last Synced: 2025-04-14T22:14:35.175Z (about 1 month ago)
- Topics: android, calligraphy, font, typeface, xamarin, xamarin-android, xamarin-android-library
- Language: C#
- Size: 155 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.codefactor.io/repository/github/nwestfall/calligraphy.xamarin)
[](https://ci.appveyor.com/project/nwestfall/calligraphy-xamarin)
[](https://badge.fury.io/nu/Calligraphy.Xamarin)Calligraphy.Xamarin
===========Port from [https://github.com/chrisjenx/Calligraph](https://github.com/chrisjenx/Calligraphy)
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.

##Getting started
### Dependency
None! Just install the nuget
### 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
```xml
```
**Note: The missing namespace, this __IS__ intentional.**### Installation
Define your default font using `CalligraphyConfig`, in your `Application` class in the `#onCreate()` method and pass it to the `CalligraphyInterceptor` that you add to your `ViewPump` builder.
```c#
public override void onCreate() {
base.onCreate();
CalligraphyConfig.InitDefault(new CalligraphyConfig.Builder()
.SetDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
.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:
```c#
protected override void AttachBaseContext(Context newBase) {
base.AttachBaseContext(CalligraphyContextWrapper.Wrap(newBase));
}
```_You're good to go!_
## Usage
### Custom font per TextView
```xml
```
### Custom font in TextAppearance
```xml
<!-- Custom Attr-->
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>```
```xml
```
### Custom font in Styles
```xml
<item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item>
```
### Custom font defined in Theme
```xml
<item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
<item name="fontPath">fonts/Roboto-ThinItalic.ttf</item>
```
#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.### 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.
```c#
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.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.
builder.SetSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SetText(builder, 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 " `) there are two standard Android widgets that have multiple methods to set typefaces. They are:
- android.support.v7.widget.SwitchCompat
- android.widget.SwitchBoth 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
- [@mironov-nsk](https://github.com/mironov-nsk)
- [@Roman Zhilich](https://github.com/RomanZhilich)
- [@Smuldr](https://github.com/Smuldr)
- [@Codebutler](https://github.com/codebutler)
- [@loganj](https://github.com/loganj)
- [@dlew](https://github.com/dlew)
- [@jbarr21](https://github.com/jbarr21)#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](https://code.google.com/p/android/issues/detail?id=88945) 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 athttp://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.