Fontain


Source link: https://github.com/scopely/fontain

Fontain

Fontain is a lightweight library for displaying text in custom fonts in your Android applications

Features

Fontain allows you to include a number of custom fonts within your app, and then access them by their Font Family, Weight, Width and Slope. It simplifies the process of selecting the proper typeface for any given usage.

Usage

Gradle

compile 'com.scopely:fontain:1.0.0'

Setup

In order for Fontain to be able to make use of your custom fonts, they must be placed in the Assets folder, with the following directory structure:

assets
  `--><font folder>

 `--><font family name>

 `--><font> 

Example:

-->assets
 `--> fonts

 |--> Helvetica
  | |--> Helvetica.otf
  | |--> Helvetica-UltraBold.otf
  | `--> Helvetica-Italic.otf
  `--> Garamond

 |--> Garamond.otf

 |--> Garamond-UltraBold.otf

 `--> Garamond-Italic.otf 

The weight, width and slope information of a particular font will be parsed from its name, so any relevant descriptors must remain in the font name. "Helvetica.otf" will be assumed to have Normal weight, Normal width and not italic. "Helvetica-UltraBold-Italic.otf" will be treated as having UltraBold weight, Normal width and italic.

Initializing

You must initialize Fontain with a Context and the name of whatever font family you want to be the default.

Fontain must be initialized before it is used (including the inflation of any of Fontain's custom Font Views), so that it may create fonts from the assets folder, but it need only be initialized once.

Fontain can be initialized with any one of the overloaded init methods:

Fontain.init(Context context, String fontsFolder, String defaultFontName) Fontain.init(Context context, String defaultFontName) Fontain.init(Context context)

Your application's onCreate() method is the recommended place to initialize Fontain.

Usage

Fontain can be used primarily in one of two ways:

Font Views

Fontain provides a number of Font Views that are extended from Android's basic text views ( TextView => FontTextView, Button => FontButton, etc). You can use these views exactly as you would use the regular android version, and on creation they will seek out the default Typeface from Fontain and apply it to themselves. Additionally these views come with custom XML attributes that allow you to specify the Font Family, Weight, Width and/or Caps Mode directly in layout XML (Slope is taken from Android's native TextStyle attribute). Example usage:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<com.scopely.fontain.views.FontTextView

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="Text"

 android:textStyle="italic"

 app:font_family="Helvetica"

 app:font_weight="ULTRA_BOLD"

 app:font_width="NARROW"

 app:caps_mode="words"/> </LinearLayout>
Caps Mode

All of the Font Views include the ability to set a Caps Mode. Doing so will initialize the view with a TransformationMethod that will display the text of the view with certain letters capitalized. Caps Mode characters will capitalize all letters, Caps Mode words will capitalize the first letter of each word, and Caps Mode sentences will capitalize the first letter of each sentence. Caps Mode title is equivalent to words except that certain words, such as 'a', 'of', etc, are not capitalized*. Caps Mode is analagous to, and is implemented with, Android's TextUtils.CAP_MODE_XXXXX, but with more granular control in xml than merely android:textAllCaps.

*Caps Mode title is only applicable to English. In non-'en' locales, Caps Mode title will behave the same as Caps Mode words.

Apply to View Hierarchy

Fontain also contains methods for walking a view hierarchy and applying a given typeface to any TextView contained therein. Fontain provides several overloaded methods that achieve the same thing:

Fontain.applyFontToViewHierarchy(View root, int weight, int width, boolean italic) Fontain.applyFontToViewHierarchy(View root, FontFamily fontFamily, int weight, int width, boolean italic) Fontain.applyFontToViewHierarchy(View root, Font font)

Fontain also has a method that will apply a font family across a view hierarchy. Whereas the above methods will apply a single font to all views in the hierarchy, the below method will select the font that best matches the view's pre-existing weight, width and slope attributes.

Fontain.applyFontFamilyToViewHierarchy(View root, FontFamily family)

Similar to the above two methods, there is also a method to apply a TransformationMethod (such as one of the caps modes) to a View hierarchy:

Fontain.applyTransformationToViewHierarchy(View root, TransformationMethod)

The use case for these methods is generally when the layout in question is provided by the system (eg: AlertDialog) or a third party library (eg: https://github.com/JakeWharton/Android-ViewPagerIndicator)

Each of these methods also has an overloaded version that takes a Predicate<TextView> that allows you to introspect each TextView in the hierarchy and return true to apply the Font/FontFamily/TransformationMethod, or false to skip the TextView in question.

Spans

Fontain also provides the following spans that allow you to change font within a single TextView:

FontSpan: applies a specified typeface to the spanned text WidthSpan: does a reverse lookup of the text's current typeface and then finds a font in the same font family with the specified width WeightSpan: does a reverse lookup of the text's current typeface and then finds a font in the same font family with the specified weight SlopeSpan: does a reverse lookup of the text's current typeface and then finds a font in the same font family with the specified slope. 

These spans all extend MetricAffectingSpan are applied in the same manner as sibling classes such as RelativeSizeSpan and SubscriptSpan.

Definitions

Weight

The weight of a font is its thickness. Bold is the classic descriptor related to weight, but there are many more, and all correspond to a numerical value on a scale that ranges from 100 to 900. The names and numerical values used by Fontain are listed below:

HAIRLINE: 100 THIN: 100 ULTRA_LIGHT: 100  EXTRA_LIGHT: 100 LIGHT: 200 BOOK: 300 NORMAL: 400 MEDIUM: 500 DEMI_BOLD: 600 SEMI_BOLD: 600 BOLD: 700 EXTRA_BOLD: 800 HEAVY: 800 BLACK: 800 ULTRA_HEAVY: 900 EXTRA_BLACK: 900 ULTRA_BLACK: 900 ULTRA_BOLD: 900 FAT: 900 POSTER: 900 

Width

The width of a font is the the horizontal space taken up by each letter relative to the letter's height. There are commonly accepted descriptors, but no standard numerical value the way there is with weight. Fontain simply maps the descriptors to a 1-5 scale. The names used by Fontain are listed below:

ULTRA_COMPRESSED: 1 EXTRA_COMPRESSED: 1 ULTRA_CONDENSED: 1 EXTRA_CONDENSED: 1 ULTRA_NARROW: 1 EXTRA_NARROW: 1 COMPRESSED: 2 CONDENSED: 2 NARROW: 2 NORMAL: 3 WIDE: 4 EXTENDED: 4 EXPANDED: 4 EXTRA_WIDE: 5 ULTRA_WIDE: 5 EXTRA_EXTENDED: 5 ULTRA_EXTENDED: 5 EXTRA_EXPANDED: 5 ULTRA_EXPANDED: 5 

Slope

The slope of a font is the angle at which the presumably vertical elements of the font rest. In Fontain this is a simple boolean: the font is either Normal or it is Italic.

Resources

Android Debug Drawer for faster development.

Simple "pull-to-update" layout.

Anvil is a tiny library to simplify android UI development. The name stands for "android virtual layout". Virtual layout is an Android analogy of the virtual DOM in the web.

EditText component which shows bouncy hint.

Plugin for JetBrains IDEs which allows to generate POJO classes using JSON code snippets.

Loading view with pretty animation.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes