TextLayoutBuilder

Support Ukraine πŸ‡ΊπŸ‡¦ Help Provide Humanitarian Aid to Ukraine .

Build text layouts easily on Android!

TextLayoutBuilder uses a builder pattern to configure the properties required to create a Layout. The methods on this builder class are similar to TextView's. On calling build(), the TextLayoutBuilder creates a text Layout based on the properties set on it.

  • Create text layouts easily.
  • Reuse builders to create similarly styled layouts.
  • Cache layouts of commonly used strings.
  • Improve performance with FreeType cache warming.
Layout layout = new TextLayoutBuilder()
    .setText("TextLayoutBuilder makes life easy")
    .setWidth(400 /*, MEASURE_MODE_EXACTLY */)
    .build();

Features

Builder

TextLayoutBuilder uses a builder pattern to configure the properties for the Layout. Gone are those days of trying to set properties on a StaticLayout.

Caching

Same properties will return same Layout on subsequent build() calls. This reduces allocation on commonly used texts.

Glyph Warming

On 4.0+ devices, TextLayoutBuilder can warm up the FreeType font cache. By drawing these glyphs on a background thread onto a Picture, TextLayoutBuilder warms these glyphs and can help reduce their render time.

Download

  • If using Gradle, add this to your build.gradle:
    compile 'com.facebook.fbui.textlayoutbuilder:textlayoutbuilder:1.7.0'
  • or, if using Maven:
    <dependency>
        <groupId>com.facebook.fbui.textlayoutbuilder</groupId>
        <artifactId>textlayoutbuilder</artifactId>
        <version>1.7.0</version>
        <typen>aar</type>
    </dependency>

Usage

  1. Set the properties on the TextLayoutBuilder:
    TextLayoutBuilder builder = new TextLayoutBuilder()
        .setText("TextLayoutBuilder makes life easy")
        .setWidth(400);
  2. Call build() on the builder to get a Layout:
    Layout layout = builder.build();
  3. Use the Layout in your code:
    layout.draw(canvas);

Additional Usage

  1. Cache the layouts for commonly used strings by turning on caching in the TextLayoutBuilder.
    mTextLayoutBuilder.setShouldCacheLayout(true);
  2. Glyph warming provides significant performance boost for large blurbs of text. Turn this on and pass in a GlyphWarmer for the TextLayoutBuilder.
    mTextLayoutBuilder
        .setShouldWarmText(true)
        .setGlyphWarmer(new GlyphWarmerImpl());
  3. Import a style defined in XML into a TextLayoutBuilder object.
    ResourceTextLayoutHelper.updateFromStyleResource(
        mTextLayoutBuilder,
        mContext,
        resId);