android.text.style.AlignmentSpan Java Examples

The following examples show how to use android.text.style.AlignmentSpan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Layout.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Get the alignment of the specified paragraph, taking into account
 * markup attached to it.
 */
public final Alignment getParagraphAlignment(int line) {
    Alignment align = mAlignment;

    if (mSpannedText) {
        Spanned sp = (Spanned) mText;
        AlignmentSpan[] spans = getParagraphSpans(sp, getLineStart(line),
                                            getLineEnd(line),
                                            AlignmentSpan.class);

        int spanLength = spans.length;
        if (spanLength > 0) {
            align = spans[spanLength-1].getAlignment();
        }
    }

    return align;
}
 
Example #2
Source File: SpanConverter.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
private void handleParagraph(int start, HtmlNode.HtmlAttr attr) {
    if (attr == null) {
        return;
    }
    setSpan(start, new StyleSpan(attr));

    Layout.Alignment align;
    if (attr.textAlign == HtmlNode.ALIGN_LEFT) {
        align = Layout.Alignment.ALIGN_NORMAL;
    } else if (attr.textAlign == HtmlNode.ALIGN_RIGHT) {
        align = Layout.Alignment.ALIGN_OPPOSITE;
    } else if (attr.textAlign == HtmlNode.ALIGN_CENTER) {
        align = Layout.Alignment.ALIGN_CENTER;
    } else {
        align = null;
    }

    if (align != null) {
        setSpan(start, position, new AlignmentSpan.Standard(align));
    }
}
 
Example #3
Source File: HtmlActivity.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Object getSpans(
        @NonNull MarkwonConfiguration configuration,
        @NonNull RenderProps renderProps,
        @NonNull HtmlTag tag) {

    final Layout.Alignment alignment;

    // html attribute without value, <align center></align>
    if (tag.attributes().containsKey("center")) {
        alignment = Layout.Alignment.ALIGN_CENTER;
    } else if (tag.attributes().containsKey("end")) {
        alignment = Layout.Alignment.ALIGN_OPPOSITE;
    } else {
        // empty value or any other will make regular alignment
        alignment = Layout.Alignment.ALIGN_NORMAL;
    }

    return new AlignmentSpan.Standard(alignment);
}
 
Example #4
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private static String getTextStyles(Spanned text, int start, int end,
                                    boolean forceNoVerticalMargin, boolean includeTextAlign) {
    String margin = null;
    String textAlign = null;
    if (forceNoVerticalMargin) {
        margin = "margin-top:0; margin-bottom:0;";
    }
    if (includeTextAlign) {
        final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
        // Only use the last AlignmentSpan with flag SPAN_PARAGRAPH
        for (int i = alignmentSpans.length - 1; i >= 0; i--) {
            AlignmentSpan s = alignmentSpans[i];
            if ((text.getSpanFlags(s) & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH) {
                final Layout.Alignment alignment = s.getAlignment();
                if (alignment == Layout.Alignment.ALIGN_NORMAL) {
                    textAlign = "text-align:start;";
                } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
                    textAlign = "text-align:center;";
                } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
                    textAlign = "text-align:end;";
                }
                break;
            }
        }
    }
    if (margin == null && textAlign == null) {
        return "";
    }
    final StringBuilder style = new StringBuilder(" style=\"");
    if (margin != null && textAlign != null) {
        style.append(margin).append(" ").append(textAlign);
    } else if (margin != null) {
        style.append(margin);
    } else if (textAlign != null) {
        style.append(textAlign);
    }
    return style.append("\"").toString();
}
 
Example #5
Source File: AlignmentSpanActivity.java    From advanced-textview with Apache License 2.0 5 votes vote down vote up
private void appendText(CharSequence text, Layout.Alignment align) {
  if (text == null || text.toString().trim().length() == 0) {
    return;
  }

  AlignmentSpan span = new AlignmentSpan.Standard(align);
  SpannableString spannableString = new SpannableString(text);
  spannableString.setSpan(span, 0, text.length(), 0);

  if (textView.length() > 0) {
    textView.append("\n\n");
  }
  textView.append(spannableString);
}
 
Example #6
Source File: AKHtml.java    From Mupdf with Apache License 2.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example #7
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private /* static */ void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for(int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #8
Source File: Html.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example #9
Source File: HtmlTagHandler.java    From v2ex with Apache License 2.0 5 votes vote down vote up
@Override
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
    if (opening) {
        // opening tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "opening, output: " + output.toString());
        }

        if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
            mListParents.add(tag);
            mListItemCount = 0;
        } else if (tag.equalsIgnoreCase("code")) {
            start(output, new Code());
        } else if (tag.equalsIgnoreCase("center")) {
            start(output, new Center());
        }
    } else {
        // closing tag
        if (HtmlTextView.DEBUG) {
            Log.d(HtmlTextView.TAG, "closing, output: " + output.toString());
        }

        if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol") || tag.equalsIgnoreCase("dd")) {
            mListParents.remove(tag);
            mListItemCount = 0;
        } else if (tag.equalsIgnoreCase("li")) {
            handleListTag(output);
        } else if (tag.equalsIgnoreCase("code")) {
            end(output, Code.class, new TypefaceSpan("monospace"), false);
        } else if (tag.equalsIgnoreCase("center")) {
            end(output, Center.class, new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), true);
        }
    }
}
 
Example #10
Source File: AKHtml.java    From Mupdf with Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #11
Source File: Html.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }

    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example #12
Source File: Html.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for(int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #13
Source File: Html.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();

    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for(int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                    ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next, option);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #14
Source File: SpanEZTest.java    From SpanEZ with Apache License 2.0 5 votes vote down vote up
@Test
public void align_center_should_add_only_one_span() {
    spanBuilder.alignCenter(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example #15
Source File: SpanEZTest.java    From SpanEZ with Apache License 2.0 5 votes vote down vote up
@Test
public void align_end_should_add_only_one_span() {
    spanBuilder.alignEnd(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example #16
Source File: WXTextDomObject.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end, int spanFlag) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE || mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end, new TextDecorationSpan(mTextDecoration), spanFlag));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor), spanFlag));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize), spanFlag));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily),
                                   spanFlag));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment), spanFlag));
    if (mLineHeight != UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight), spanFlag));
    }
  }
  return ops;
}
 
Example #17
Source File: HtmlCompat.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(Context context, StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] styles = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;
        for (ParagraphStyle style : styles) {
            if (style instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }
        withinDiv(context, out, text, i, next, option);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #18
Source File: SpanEZTest.java    From SpanEZ with Apache License 2.0 5 votes vote down vote up
@Test
public void align_start_should_add_only_one_span() {
    spanBuilder.alignStart(paragraph)
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(AlignmentSpan.class));
}
 
Example #19
Source File: HtmlToSpannedConverter.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
private void endBlockElement(String tag, Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }
    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(tag, text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example #20
Source File: WXTextDomObject.java    From weex-uikit with MIT License 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end, int spanFlag) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE) {
      ops.add(new SetSpanOperation(start, end,
                                   new UnderlineSpan(), spanFlag));
    }
    if (mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end,
                                   new StrikethroughSpan(), spanFlag));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor), spanFlag));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize), spanFlag));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily),
                                   spanFlag));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment), spanFlag));
    if (mLineHeight != UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight), spanFlag));
    }
  }
  return ops;
}
 
Example #21
Source File: MainActivity.java    From 600SeriesAndroidUploader with MIT License 5 votes vote down vote up
private void chartViewAdjusted() {
    chartChangeTimestamp = System.currentTimeMillis();

    long end = timeLastSGV == 0 ? chartChangeTimestamp - chartTimeOffset : timeLastSGV - chartTimeOffset;
    long start = end - chartZoom * 60 * 60000L;

    String t = String.format(getString(R.string.main_screen__value_hour_chart), chartZoom);

    String m = String.format("\n%s %s",
            FormatKit.getInstance().formatAsMonthName(start),
            FormatKit.getInstance().formatAsDay(start)
    );

    String d = String.format("\n%s - %s",
            FormatKit.getInstance().formatAsDayClock(start),
            FormatKit.getInstance().formatAsDayClock(end)
    );

    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(t);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - t.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new StyleSpan(Typeface.BOLD), ssb.length() - t.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ssb.append(m);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - m.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new RelativeSizeSpan(0.75f), ssb.length() - m.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ssb.append(d);
    ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), ssb.length() - d.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new RelativeSizeSpan(0.85f), ssb.length() - d.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    toast = serveToast(ssb, toast, findViewById(R.id.view_sgv));

    refreshDisplayChart();
}
 
Example #22
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
    int len = text.length();
    int next;
    for (int i = 0; i < len; i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }
        withinDiv(out, text, i, next, option);
        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #23
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static String getTextStyles(Spanned text, int start, int end,
                                    boolean forceNoVerticalMargin, boolean includeTextAlign) {
    String margin = null;
    String textAlign = null;
    if (forceNoVerticalMargin) {
        margin = "margin-top:0; margin-bottom:0;";
    }
    if (includeTextAlign) {
        final AlignmentSpan[] alignmentSpans = text.getSpans(start, end, AlignmentSpan.class);
        // Only use the last AlignmentSpan with flag SPAN_PARAGRAPH
        for (int i = alignmentSpans.length - 1; i >= 0; i--) {
            AlignmentSpan s = alignmentSpans[i];
            if ((text.getSpanFlags(s) & Spanned.SPAN_PARAGRAPH) == Spanned.SPAN_PARAGRAPH) {
                final Layout.Alignment alignment = s.getAlignment();
                if (alignment == Layout.Alignment.ALIGN_NORMAL) {
                    textAlign = "text-align:start;";
                } else if (alignment == Layout.Alignment.ALIGN_CENTER) {
                    textAlign = "text-align:center;";
                } else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
                    textAlign = "text-align:end;";
                }
                break;
            }
        }
    }
    if (margin == null && textAlign == null) {
        return "";
    }
    final StringBuilder style = new StringBuilder(" style=\"");
    if (margin != null && textAlign != null) {
        style.append(margin).append(" ").append(textAlign);
    } else if (margin != null) {
        style.append(margin);
    } else if (textAlign != null) {
        style.append(textAlign);
    }
    return style.append("\"").toString();
}
 
Example #24
Source File: Html.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
private static void endBlockElement(Editable text) {
    Newline n = getLast(text, Newline.class);
    if (n != null) {
        appendNewlines(text, n.mNumNewlines);
        text.removeSpan(n);
    }
    Alignment a = getLast(text, Alignment.class);
    if (a != null) {
        setSpanFromMark(text, a, new AlignmentSpan.Standard(a.mAlignment));
    }
}
 
Example #25
Source File: Html.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for(int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #26
Source File: TextDecorator.java    From text-decorator with Apache License 2.0 5 votes vote down vote up
public TextDecorator alignText(final Layout.Alignment alignment, final String... texts) {
  int index;

  for (String text : texts) {
    if (content.contains(text)) {
      index = content.indexOf(text);
      decoratedContent.setSpan(new AlignmentSpan.Standard(alignment), index, index + text.length(), flags);
    }
  }

  return this;
}
 
Example #27
Source File: MyHtml.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private static void withinHtml(StringBuilder out, Spanned text) {
    int len = text.length();

    int next;
    for (int i = 0; i < text.length(); i = next) {
        next = text.nextSpanTransition(i, len, ParagraphStyle.class);
        ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
        String elements = " ";
        boolean needDiv = false;

        for(int j = 0; j < style.length; j++) {
            if (style[j] instanceof AlignmentSpan) {
                Layout.Alignment align =
                        ((AlignmentSpan) style[j]).getAlignment();
                needDiv = true;
                if (align == Layout.Alignment.ALIGN_CENTER) {
                    elements = "align=\"center\" " + elements;
                } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
                    elements = "align=\"right\" " + elements;
                } else {
                    elements = "align=\"left\" " + elements;
                }
            }
        }
        if (needDiv) {
            out.append("<div ").append(elements).append(">");
        }

        withinDiv(out, text, i, next);

        if (needDiv) {
            out.append("</div>");
        }
    }
}
 
Example #28
Source File: FrameResolver.java    From Tehreer-Android with Apache License 2.0 5 votes vote down vote up
float computeFlushFactor() {
    Layout.Alignment alignment = null;

    // Get the top most alignment.
    for (int i = paragraphSpans.length - 1; i >= 0; i--) {
        if (paragraphSpans[i] instanceof AlignmentSpan) {
            alignment = ((AlignmentSpan) paragraphSpans[i]).getAlignment();
            break;
        }
    }

    return getFlushFactor(alignment, baseLevel);
}
 
Example #29
Source File: WXTextDomObject.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * Create a task list which contains {@link SetSpanOperation}. The task list will be executed
 * in other method.
 * @param end the end character of the text.
 * @return a task list which contains {@link SetSpanOperation}.
 */
private List<SetSpanOperation> createSetSpanOperation(int end) {
  List<SetSpanOperation> ops = new LinkedList<>();
  int start = 0;
  if (end >= start) {
    if (mTextDecoration == WXTextDecoration.UNDERLINE) {
      ops.add(new SetSpanOperation(start, end,
                                   new UnderlineSpan()));
    }
    if (mTextDecoration == WXTextDecoration.LINETHROUGH) {
      ops.add(new SetSpanOperation(start, end,
                                   new StrikethroughSpan()));
    }
    if (mIsColorSet) {
      ops.add(new SetSpanOperation(start, end,
                                   new ForegroundColorSpan(mColor)));
    }
    if (mFontSize != UNSET) {
      ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize)));
    }
    if (mFontStyle != UNSET
        || mFontWeight != UNSET
        || mFontFamily != null) {
      ops.add(new SetSpanOperation(start, end,
                                   new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily)));
    }
    ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment)));
    if(mLineHeight !=UNSET) {
      ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight)));
    }
  }
  return ops;
}
 
Example #30
Source File: SpanConverter.java    From Ruisi with Apache License 2.0 5 votes vote down vote up
private void handleBlockTag(boolean isStart, int type, int start, HtmlNode.HtmlAttr attr) {
    if (position <= 0) {
        return;
    }
    if (spannedBuilder.charAt(position - 1) != '\n') {
        spannedBuilder.append('\n');
        position++;
    }

    //结束block 标签
    if (!isStart && attr != null) {
        Layout.Alignment align;
        if (attr.align == HtmlNode.ALIGN_LEFT) {
            align = Layout.Alignment.ALIGN_NORMAL;
        } else if (attr.align == HtmlNode.ALIGN_RIGHT) {
            align = Layout.Alignment.ALIGN_OPPOSITE;
        } else if (attr.align == HtmlNode.ALIGN_CENTER) {
            align = Layout.Alignment.ALIGN_CENTER;
        } else {
            align = null;
        }

        if (align != null) {
            setSpan(start, position, new AlignmentSpan.Standard(align));
        }
    }
}