public static StaticLayout make()

in library/src/main/java/com/facebook/fbui/textlayoutbuilder/StaticLayoutHelper.java [165:293]


  public static StaticLayout make(
      CharSequence text,
      int start,
      int end,
      TextPaint paint,
      int width,
      Layout.Alignment alignment,
      float spacingMult,
      float spacingAdd,
      boolean includePadding,
      TextUtils.TruncateAt ellipsize,
      int ellipsisWidth,
      int maxLines,
      TextDirectionHeuristicCompat textDirection,
      int breakStrategy,
      int hyphenationFrequency,
      int justificationMode,
      int[] leftIndents,
      int[] rightIndents,
      boolean useLineSpacingFromFallbacks) {

    if (Build.VERSION.SDK_INT >= 23) {
      StaticLayout.Builder builder =
          StaticLayout.Builder.obtain(text, start, end, paint, width)
              .setAlignment(alignment)
              .setLineSpacing(spacingAdd, spacingMult)
              .setIncludePad(includePadding)
              .setEllipsize(ellipsize)
              .setEllipsizedWidth(ellipsisWidth)
              .setMaxLines(maxLines)
              .setTextDirection(StaticLayoutProxy.fromTextDirectionHeuristicCompat(textDirection))
              .setBreakStrategy(breakStrategy)
              .setHyphenationFrequency(hyphenationFrequency)
              .setIndents(leftIndents, rightIndents);

      if (Build.VERSION.SDK_INT >= 26) {
        builder.setJustificationMode(justificationMode);
      }

      if (Build.VERSION.SDK_INT >= 28) {
        builder.setUseLineSpacingFromFallbacks(useLineSpacingFromFallbacks);
      }

      return builder.build();
    }

    StaticLayout layout =
        getStaticLayoutMaybeMaxLines(
            text,
            start,
            end,
            paint,
            width,
            alignment,
            spacingMult,
            spacingAdd,
            includePadding,
            ellipsize,
            ellipsisWidth,
            maxLines,
            textDirection);

    // Returned layout may not have correct line count (either because it is not supported
    // pre-ICS, or because there is a bug in Android pre-Lollipop that causes the text to span
    // over more lines than we asked for). We need to manually check if that happened and
    // re-create Layout with a substring that will fit into required number of lines.
    if (maxLines > 0) {
      while (layout.getLineCount() > maxLines) {
        int newEnd = layout.getLineStart(maxLines);
        if (newEnd >= end) {
          // to break out of a potential infinite loop
          break;
        }

        // newEnd is where the next line starts, not where the previous line ends
        // we need to skip over the whitespace characters to get to the end of the line
        while (newEnd > start) {
          if (Character.isSpace(text.charAt(newEnd - 1))) {
            --newEnd;
          } else {
            break;
          }
        }

        end = newEnd;

        layout =
            getStaticLayoutMaybeMaxLines(
                text,
                start,
                end,
                paint,
                width,
                alignment,
                spacingMult,
                spacingAdd,
                includePadding,
                ellipsize,
                ellipsisWidth,
                maxLines,
                textDirection);

        if (layout.getLineCount() >= maxLines && layout.getEllipsisCount(maxLines - 1) == 0) {
          CharSequence ellipsizedText = text.subSequence(start, end) + SPACE_AND_ELLIPSIS;
          layout =
              getStaticLayoutMaybeMaxLines(
                  ellipsizedText,
                  0,
                  ellipsizedText.length(),
                  paint,
                  width,
                  alignment,
                  spacingMult,
                  spacingAdd,
                  includePadding,
                  ellipsize,
                  ellipsisWidth,
                  maxLines,
                  textDirection);
        }
      }
    }

    while (!fixLayout(layout)) {
      // try again
    }

    return layout;
  }