Search in sources :

Example 1 with AreUnderlineSpan

use of com.chinalwb.are.spans.AreUnderlineSpan in project Android-Rich-text-Editor by chinalwb.

the class AREditText method onSelectionChanged.

/*
	 * ----------------------------------------- * Rich Text Style Area
	 * -----------------------------------------
	 */
@Override
public void onSelectionChanged(int selStart, int selEnd) {
    if (sToolbar == null) {
        return;
    }
    Util.log(" on Selection changed == " + this);
    boolean boldExists = false;
    boolean italicsExists = false;
    boolean underlinedExists = false;
    boolean striketrhoughExists = false;
    boolean backgroundColorExists = false;
    boolean quoteExists = false;
    // 
    // Two cases:
    // 1. Selection is just a pure cursor
    // 2. Selection is a range
    Editable editable = this.getEditableText();
    if (selStart > 0 && selStart == selEnd) {
        CharacterStyle[] styleSpans = editable.getSpans(selStart - 1, selStart, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    boldExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    italicsExists = true;
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                // TODO
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                underlinedExists = true;
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                striketrhoughExists = true;
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                backgroundColorExists = true;
            }
        }
        QuoteSpan[] quoteSpans = editable.getSpans(selStart - 1, selStart, QuoteSpan.class);
        if (quoteSpans != null && quoteSpans.length > 0) {
            quoteExists = true;
        }
    // To check Quote!!
    } else {
        // 
        // Selection is a range
        CharacterStyle[] styleSpans = editable.getSpans(selStart, selEnd, CharacterStyle.class);
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i] instanceof StyleSpan) {
                if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        boldExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                    }
                } else if (((StyleSpan) styleSpans[i]).getStyle() == android.graphics.Typeface.BOLD_ITALIC) {
                    if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                        italicsExists = true;
                        boldExists = true;
                    }
                }
            } else if (styleSpans[i] instanceof AreUnderlineSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    underlinedExists = true;
                }
            } else if (styleSpans[i] instanceof StrikethroughSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    striketrhoughExists = true;
                }
            } else if (styleSpans[i] instanceof BackgroundColorSpan) {
                if (editable.getSpanStart(styleSpans[i]) <= selStart && editable.getSpanEnd(styleSpans[i]) >= selEnd) {
                    backgroundColorExists = true;
                }
            }
        }
    }
    QuoteSpan[] quoteSpans = editable.getSpans(selStart, selEnd, QuoteSpan.class);
    if (quoteSpans != null && quoteSpans.length > 0) {
        if (editable.getSpanStart(quoteSpans[0]) <= selStart && editable.getSpanEnd(quoteSpans[0]) >= selEnd) {
            quoteExists = true;
        }
    }
    // 
    // Set style checked status
    ARE_Helper.updateCheckStatus(sToolbar.getBoldStyle(), boldExists);
    ARE_Helper.updateCheckStatus(sToolbar.getItalicStyle(), italicsExists);
    ARE_Helper.updateCheckStatus(sToolbar.getUnderlineStyle(), underlinedExists);
    ARE_Helper.updateCheckStatus(sToolbar.getStrikethroughStyle(), striketrhoughExists);
    ARE_Helper.updateCheckStatus(sToolbar.getBackgroundColoStyle(), backgroundColorExists);
    ARE_Helper.updateCheckStatus(sToolbar.getQuoteStyle(), quoteExists);
}
Also used : AreUnderlineSpan(com.chinalwb.are.spans.AreUnderlineSpan) StyleSpan(android.text.style.StyleSpan) Editable(android.text.Editable) QuoteSpan(android.text.style.QuoteSpan) CharacterStyle(android.text.style.CharacterStyle) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Aggregations

Editable (android.text.Editable)1 BackgroundColorSpan (android.text.style.BackgroundColorSpan)1 CharacterStyle (android.text.style.CharacterStyle)1 QuoteSpan (android.text.style.QuoteSpan)1 StrikethroughSpan (android.text.style.StrikethroughSpan)1 StyleSpan (android.text.style.StyleSpan)1 AreUnderlineSpan (com.chinalwb.are.spans.AreUnderlineSpan)1