Build text Layouts easily on Android.
- Create text
Layouts easily. - Reuse builders to create similarly styled
Layouts. - Cache
Layouts of commonly used strings. - Improve performance by warming up the FreeType cache.
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>
<type>aar</type>
</dependency>- Set the properties on the
TextLayoutBuilder:
TextLayoutBuilderbuilder = newTextLayoutBuilder()
.setText("TextLayoutBuilder makes life easy")
.setTextColor(Color.BLUE)
.setWidth(400/*, MEASURE_MODE_EXACTLY */);- Call
build()on the builder to get aLayout:
Layoutlayout = builder.build();- Use the
Layoutin your code:
publicclassCustomViewextendsView {
privateLayoutlayout;
publicCustomView(Contextcontext) {
super(context);
}
publicvoidsetLayout(Layoutlayout) {
this.layout = layout;
}
@OverrideprotectedvoidonDraw(Canvascanvas) {
super.onDraw(canvas);
// Draw the layout.layout.draw(canvas);
}
}- Cache the layouts for commonly used strings by turning on caching in the
TextLayoutBuilder.
textLayoutBuilder.setShouldCacheLayout(true);- Glyph warming provides significant performance boost for large blurbs of text.
Turn this on and pass in a
GlyphWarmerfor theTextLayoutBuilder.
textLayoutBuilder
.setShouldWarmText(true)
.setGlyphWarmer(newGlyphWarmerImpl());- Import a style defined in XML into a
TextLayoutBuilderobject.
ResourceTextLayoutHelper.updateFromStyleResource(
textLayoutBuilder, // builder objectcontext, // Activity contextresId); // style resource idTextLayoutBuilder is Apache-2-licensed.
