Exemplo n.º 1
0
  private AttributedString prepareAttributedString() {
    List<StyledText> textChunks = getTextChunks();

    StringBuilder buf = new StringBuilder();

    for (StyledText textChunk : textChunks) {
      buf.append(textChunk.getText());
    }

    AttributedString attributedString = new AttributedString(buf.toString());

    int startIndex = 0;
    int endIndex = 0;

    for (StyledText textChunk : textChunks) {
      if (textChunk == textChunks.get(textChunks.size() - 1)) endIndex = buf.length();
      else endIndex = startIndex + textChunk.getText().length();

      attributedString.addAttribute(TextAttribute.FONT, textChunk.getFont(), startIndex, endIndex);
      attributedString.addAttribute(
          TextAttribute.FOREGROUND, textChunk.getColor(), startIndex, endIndex);

      startIndex += textChunk.getText().length();
    }

    return attributedString;
  }