TextLayoutBuilder
Build text Layouts easily on Android.
Features
- Create text
Layout
s easily. - Reuse builders to create similarly styled
Layout
s. - Cache
Layout
s of commonly used strings. - Improve performance by warming up the FreeType cache.
Download
If using Gradle, add this to your build.gradle
:
compile 'com.facebook.fbui.textlayoutbuilder:textlayoutbuilder:1.1.0'
or, if using Maven:
<dependency>
<groupId>com.facebook.fbui.textlayoutbuilder</groupId>
<artifactId>textlayoutbuilder</artifactId>
<version>1.1.0</version>
<type>aar</type> </dependency>
Usage
- Set the properties on the
TextLayoutBuilder
:
TextLayoutBuilder builder = new TextLayoutBuilder()
.setTextAppearance(context, resId)
.setText("TextLayoutBuilder makes life easy")
.setWidth(400 /*, MEASURE_MODE_EXACTLY */);
- Call
build()
on the builder to get aLayout
:
Layout layout = builder.build();
- Use the
Layout
in your code:
public class CustomView extends View {
private Layout mLayout;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setLayout(Layout layout) {
mLayout = layout;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw the layout.
mLayout.draw(canvas);
}
}
Additional Usage
- Cache the layouts for commonly used strings by turning on caching in the
TextLayoutBuilder
.
mTextLayoutBuilder.setShouldCacheLayout(true);
- Glyph warming provides significant performance boost for large blurbs of text. Turn this on and pass in a
GlyphWarmer
for theTextLayoutBuilder
.
mTextLayoutBuilder
.setShouldWarmText(true)
.setGlyphWarmer(new GlyphWarmerImpl());
- Import a style defined in XML into a
TextLayoutBuilder
object.
ResourceTextLayoutHelper.updateFromStyleResource(
mTextLayoutBuilder, // builder object
mContext,
// Activity context
resId);
// style resource id