Coder Social home page Coder Social logo

facebook / textlayoutbuilder Goto Github PK

View Code? Open in Web Editor NEW
1.5K 49.0 141.0 529 KB

An Android library that allows you to build text layouts more easily.

Home Page: https://facebook.github.io/TextLayoutBuilder

License: Apache License 2.0

Java 94.71% Kotlin 5.29%

textlayoutbuilder's Introduction

TextLayoutBuilder

Build text Layouts easily on Android.

Features

  • 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.

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>
  <type>aar</type>
</dependency>

Usage

  1. Set the properties on the TextLayoutBuilder:
TextLayoutBuilder builder = new TextLayoutBuilder()
    .setText("TextLayoutBuilder makes life easy")
    .setTextColor(Color.BLUE)
    .setWidth(400 /*, MEASURE_MODE_EXACTLY */);
  1. Call build() on the builder to get a Layout:
Layout layout = builder.build();
  1. Use the Layout in your code:
public class CustomView extends View {
    private Layout layout;

    public CustomView(Context context) {
        super(context);
    }

    public void setLayout(Layout layout) {
        this.layout = layout;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Draw the layout.
        layout.draw(canvas);
    }
}

Additional Usage

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

License

TextLayoutBuilder is Apache-2-licensed.

textlayoutbuilder's People

Contributors

astreet avatar brosenfeld avatar cgrushko avatar coreywu avatar dmitryvinn avatar dulinriley avatar ianchilds avatar jaegs avatar mishrabhilash avatar mkarpio avatar muraziz avatar passy avatar sjkirby avatar sriramramani avatar strulovich avatar xiphirx avatar zertosh avatar zpao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

textlayoutbuilder's Issues

Creation of StaticLayout

When you create the layout you try to handle the different scenario. Each of case happens of the specific version of API?

Gradle fails to resolve version 1.3.1

Hello!

Maybe this is intentional but.. did someone forget to upload version 1.3.1?

The readme, the website and the commits say the latest version is 1.3.1, but I get Failed to resolve: com.facebook.fbui.textlayoutbuilder:textlayoutbuilder:1.3.1 when I try to sync my project.

Thanks!

Layout on background thread?

Is code like the below thread safe?

Layout layout = new TextLayoutBuilder()
    .setTextAppearance(context, resId)
    .setText("TextLayoutBuilder makes life easy")
    .setWidth(400)
    .layout();

My understanding is that Litho does background layout and measurement. And Litho uses TextLayoutHelper under the hood for some things. So I'm wondering if this library can be used to write a custom view that does all its measurement and layout on a background thread (assuming I already know max size).

No support for letter spacing

I can see this project is being used in Litho as well. From there, I came searching here for letter spacing support as it is in Android TextView.
Are there any plans to this in future?

Restrictions on non-SDK interfaces

#3: Linking greylist-max-p Landroid/text/StaticLayout;->(Ljava/lang/CharSequence;IILandroid/text/TextPaint;ILandroid/text/Layout$Alignment;Landroid/text/TextDirectionHeuristic;FFZLandroid/text/TextUtils$TruncateAt;II)V use(s):
Lcom/facebook/fbui/textlayoutbuilder/proxy/StaticLayoutProxy;->create(Ljava/lang/CharSequence;IILandroid/text/TextPaint;ILandroid/text/Layout$Alignment;FFZLandroid/text/TextUtils$TruncateAt;IILandroidx/core/text/TextDirectionHeuristicCompat;)Landroid/text/StaticLayout; (2 occurrences)

the constructor method is Deprecated, should be instead of Builder.

Usage 3 the method onDraw is wrong

in usage 3
it is wrong that the first line of the method of onDraw ?
it has a error of java.lang.StackOverflowError.
it should be super.onDraw(canvas);

Set alignment is not work

I use library 1.2.0.
The alignment always is ALIGN_NORMAL
This is my code.
screenshot_20180131-103849

And I see the setTextAppearance(context, resId) in website , but I can't use in TextLayoutBuilder.

Thank you.

the hide constructor method of android.text.StaticLayout

android.text.StaticLayout

the constructor method with 'int maxLines' param is a hide method.
Does StaticLayoutProxy.java can new StaticLayout object with the 'maxLines' directly?

    /**
     * @hide
     */
    public StaticLayout(CharSequence source, int bufstart, int bufend,
                        TextPaint paint, int outerwidth,
                        Alignment align, TextDirectionHeuristic textDir,
                        float spacingmult, float spacingadd,
                        boolean includepad,
                        TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int maxLines) {

Why infinity loop?

while (true) {
        try {
          layout = StaticLayoutHelper.make(
              mParams.text,
              0,
              mParams.text.length(),
              mParams.paint,
              width,
              mParams.alignment,
              mParams.spacingMult,
              mParams.spacingAdd,
              mParams.includePadding,
              mParams.ellipsize,
              width,
              numLines,
              mParams.textDirection);
        } catch (IndexOutOfBoundsException e) {
          // Workaround for https://code.google.com/p/android/issues/detail?id=35412
          if (!(mParams.text instanceof String)) {
            // remove all Spannables and re-try
            Log.e("TextLayoutBuilder", "Hit bug #35412, retrying with Spannables removed", e);
            mParams.text = mParams.text.toString();
            continue;
          } else {
            // If it still happens with all Spannables removed we'll bubble the exception up
            throw e;
          }
        }

        break;
      }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.