Example usage for android.text Editable setSpan

List of usage examples for android.text Editable setSpan

Introduction

In this page you can find the example usage for android.text Editable setSpan.

Prototype

public void setSpan(Object what, int start, int end, int flags);

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:Main.java

/**
 * @see android.text.Html//from w  w  w. ja  va  2 s .  co m
 */
private static void start(Editable text, Object mark) {
    int len = text.length();
    text.setSpan(mark, len, len, Spanned.SPAN_MARK_MARK);
}

From source file:Main.java

/**
 * Modified from {@link android.text.Html}
 *//*from   ww w .  j  a  v  a 2s  . co  m*/
private static void end(Editable text, Class<?> kind, Object... replaces) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);
    if (where != len) {
        for (Object replace : replaces) {
            text.setSpan(replace, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:org.eyeseetea.malariacare.network.CustomParser.java

/** Modified from {@link android.text.Html} */
private static void end(Editable text, Class<?> kind, Object... replaces) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);/* w w w. ja  v  a 2s  .  c  o  m*/
    if (where != len) {
        for (Object replace : replaces) {
            text.setSpan(replace, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return;
}

From source file:com.duy.pascal.ui.editor.highlight.StringHighlighter.java

@Override
public void highlight(@NonNull Editable allText, @NonNull CharSequence textToHighlight, int start) {
    mStringRegion.clear();//  ww  w . j a v  a 2  s  .c  om
    for (Matcher m = STRINGS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getStringColor()), start + m.start(), start + m.end(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mStringRegion.add(new Pair<>(start + m.start(), start + m.end()));
    }
}

From source file:com.duy.pascal.ui.editor.highlight.CommentHighlighter.java

@Override
public void highlight(@NonNull Editable allText, @NonNull CharSequence textToHighlight, int start) {
    mCommentRegion.clear();/*from  www  . j  a  v a2s.  co m*/
    for (Matcher m = COMMENTS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getCommentColor()), start + m.start(),
                start + m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCommentRegion.add(new Pair<>(start + m.start(), start + m.end()));
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void start(Editable text, Object mark) {
    int len = text.length();
    text.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.duy.pascal.ui.editor.highlight.CodeHighlighter.java

private void highlightOther(@NonNull Editable allText, @NonNull CharSequence textToHighlight, int start) {
    //high light number
    for (Matcher m = NUMBERS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getNumberColor()), start + m.start(), start + m.end(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }//from w  ww .j  av  a2 s. c om
    for (Matcher m = KEYWORDS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getKeywordColor()), start + m.start(),
                start + m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    for (Matcher m = BUILTIN_FUNCTIONS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getKeywordColor()), start + m.start(),
                start + m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    for (Matcher m = SYMBOLS.matcher(textToHighlight); m.find();) {
        allText.setSpan(new ForegroundColorSpan(codeTheme.getOptColor()), start + m.start(), start + m.end(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    for (Matcher m = RGB_FUNCTION.matcher(textToHighlight); m.find();) {
        try {
            int r = Integer.parseInt(m.group(3).trim());
            int g = Integer.parseInt(m.group(5).trim());
            int b = Integer.parseInt(m.group(7).trim());
            int back = Color.rgb(r, g, b);
            int fore = Color.rgb(255 - r, 255 - g, 255 - b);

            allText.setSpan(new BackgroundColorSpan(back), start + m.start(1), start + m.end(1),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            allText.setSpan(new ForegroundColorSpan(fore), start + m.start(1), start + m.end(1),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } catch (Exception e) {

        }
    }
    for (Matcher m = ARGB_FUNCTION.matcher(textToHighlight); m.find();) {
        try {
            int r = Integer.parseInt(m.group(3).trim());
            int g = Integer.parseInt(m.group(5).trim());
            int b = Integer.parseInt(m.group(7).trim());
            int back = Color.rgb(r, g, b);
            int fore = Color.rgb(255 - r, 255 - g, 255 - b);

            allText.setSpan(new BackgroundColorSpan(back), start + m.start(1), start + m.end(1),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            allText.setSpan(new ForegroundColorSpan(fore), start + m.start(1), start + m.end(1),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } catch (Exception e) {

        }
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void startA(Editable text, Attributes attributes) {
    String href = attributes.getValue("", "href");

    int len = text.length();
    text.setSpan(new Href(href), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:org.miaowo.miaowo.util.Html.java

private static void startBlockElement(Editable text, Attributes attributes, int margin) {
    final int len = text.length();
    if (margin > 0) {
        appendNewlines(text, margin);/*from w  w w  . j av a  2  s .co  m*/
        text.setSpan(new Newline(margin), len, len, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    String style = attributes.getValue("", "style");
    if (style != null) {
        Matcher m = getTextAlignPattern().matcher(style);
        if (m.find()) {
            String alignment = m.group(1);
            if (alignment.equalsIgnoreCase("start")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_NORMAL), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            } else if (alignment.equalsIgnoreCase("center")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_CENTER), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            } else if (alignment.equalsIgnoreCase("end")) {
                text.setSpan(new Alignment(Layout.Alignment.ALIGN_OPPOSITE), len, len,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

From source file:org.miaowo.miaowo.util.Html.java

private static void startFont(Editable text, Attributes attributes) {
    String color = attributes.getValue("", "color");
    String face = attributes.getValue("", "face");

    int len = text.length();
    text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}