private void build(int width) {

    positions.clear();
    width = width - getPaddingLeft() - getPaddingRight();

    if (width <= 0 || TextUtils.isEmpty(text)) {
      truncatedLayout = expandedLayout = null;
      return;
    }

    for (BubbleSpan span : spans) {
      span.resetWidth(width);
    }

    try {

      truncatedLayout =
          expandedLayout =
              new StaticLayout(
                  text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, lineSpacing, 1, false);

      if (maxLines > 0 && truncatedLayout.getLineCount() > maxLines) {

        int lineEnd = truncatedLayout.getLineEnd(maxLines - 1);
        int offset = -1;
        StaticLayout sl =
            new StaticLayout(
                moreText, textPaint, width, Layout.Alignment.ALIGN_NORMAL, lineSpacing, 1, false);

        sl.getWidth();
        while (truncatedLayout.getLineCount() > maxLines && lineEnd > 0) {

          if (offset == -1
              && truncatedLayout.getLineWidth(maxLines - 1) + sl.getLineWidth(0) > width) {

            offset =
                truncatedLayout.getOffsetForHorizontal(maxLines - 1, width - sl.getLineWidth(0));

            lineEnd = offset;

          } else if (offset > 0) {
            lineEnd--;
          }

          SpannableStringBuilder textTruncated =
              new SpannableStringBuilder(text.subSequence(0, lineEnd));
          textTruncated.append(moreText);

          truncatedLayout =
              new StaticLayout(
                  textTruncated,
                  textPaint,
                  width,
                  Layout.Alignment.ALIGN_NORMAL,
                  lineSpacing,
                  1,
                  false);
        }
      }
    } catch (java.lang.ArrayIndexOutOfBoundsException e) {
      return;
    }

    if (truncated) {
      recomputeSpans((Spannable) truncatedLayout.getText());
    } else {
      recomputeSpans((Spannable) expandedLayout.getText());
    }

    for (BubbleSpan span : spans) {
      positions.put(span, span.rect(this));
    }
  }
Example #2
0
 public void setText(String text) {
   SpannableStringBuilder ss = new SpannableStringBuilder(text);
   ss.setSpan(new EditorLineNumberSpan(), 0, ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
   super.setText(ss);
 }