Search in sources :

Example 16 with CharacterStyle

use of android.text.style.CharacterStyle in project PhoneProfiles by henrichg.

the class ProfilePreferencesNestedFragment method setTitleStyle.

private void setTitleStyle(Preference preference, boolean bold, boolean underline, boolean systemSettings) {
    if (preference != null) {
        CharSequence title = preference.getTitle();
        if (systemSettings) {
            String s = title.toString();
            if (!s.contains("(S)"))
                title = TextUtils.concat("(S) ", title);
        }
        Spannable sbt = new SpannableString(title);
        Object[] spansToRemove = sbt.getSpans(0, title.length(), Object.class);
        for (Object span : spansToRemove) {
            if (span instanceof CharacterStyle)
                sbt.removeSpan(span);
        }
        if (bold || underline) {
            if (bold)
                sbt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (underline)
                sbt.setSpan(new UnderlineSpan(), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            preference.setTitle(sbt);
        } else {
            preference.setTitle(sbt);
        }
    }
}
Also used : SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) SpannableString(android.text.SpannableString) Spannable(android.text.Spannable) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan)

Example 17 with CharacterStyle

use of android.text.style.CharacterStyle in project PhoneProfiles by henrichg.

the class PhoneProfilesPreferencesNestedFragment method setPreferenceTitleStyle.

private static void setPreferenceTitleStyle(Preference preference, boolean bold, /*boolean underline,*/
boolean errorColor) {
    if (preference != null) {
        CharSequence title = preference.getTitle();
        Spannable sbt = new SpannableString(title);
        Object[] spansToRemove = sbt.getSpans(0, title.length(), Object.class);
        for (Object span : spansToRemove) {
            if (span instanceof CharacterStyle)
                sbt.removeSpan(span);
        }
        if (bold) /* || underline*/
        {
            // if (bold)
            sbt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            /*if (underline)
                    sbt.setSpan(new UnderlineSpan(), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/
            if (errorColor)
                sbt.setSpan(new ForegroundColorSpan(Color.RED), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            preference.setTitle(sbt);
        } else {
            preference.setTitle(sbt);
        }
    }
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) Spannable(android.text.Spannable) CharacterStyle(android.text.style.CharacterStyle)

Example 18 with CharacterStyle

use of android.text.style.CharacterStyle in project ForPDA by RadiationX.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = App.get();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 19 with CharacterStyle

use of android.text.style.CharacterStyle in project PhoneProfilesPlus by henrichg.

the class GlobalGUIRoutines method setPreferenceTitleStyle.

public static void setPreferenceTitleStyle(Preference preference, boolean bold, boolean underline, boolean errorColor, boolean systemSettings) {
    if (preference != null) {
        CharSequence title = preference.getTitle();
        if (systemSettings) {
            String s = title.toString();
            if (!s.contains("(S)"))
                title = TextUtils.concat("(S) ", title);
        }
        Spannable sbt = new SpannableString(title);
        Object[] spansToRemove = sbt.getSpans(0, title.length(), Object.class);
        for (Object span : spansToRemove) {
            if (span instanceof CharacterStyle)
                sbt.removeSpan(span);
        }
        if (bold || underline) {
            if (bold)
                sbt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (underline)
                sbt.setSpan(new UnderlineSpan(), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (errorColor)
                sbt.setSpan(new ForegroundColorSpan(Color.RED), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            preference.setTitle(sbt);
        } else {
            preference.setTitle(sbt);
        }
    }
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) SpannableString(android.text.SpannableString) Spannable(android.text.Spannable) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan)

Example 20 with CharacterStyle

use of android.text.style.CharacterStyle in project android-ocr by rmtheis.

the class CaptureActivity method setSpanBetweenTokens.

/**
 * Given either a Spannable String or a regular String and a token, apply
 * the given CharacterStyle to the span between the tokens.
 *
 * NOTE: This method was adapted from:
 *  http://www.androidengineer.com/2010/08/easy-method-for-formatting-android.html
 *
 * <p>
 * For example, {@code setSpanBetweenTokens("Hello ##world##!", "##", new
 * ForegroundColorSpan(0xFFFF0000));} will return a CharSequence {@code
 * "Hello world!"} with {@code world} in red.
 */
private CharSequence setSpanBetweenTokens(CharSequence text, String token, CharacterStyle... cs) {
    // Start and end refer to the points where the span will apply
    int tokenLen = token.length();
    int start = text.toString().indexOf(token) + tokenLen;
    int end = text.toString().indexOf(token, start);
    if (start > -1 && end > -1) {
        // Copy the spannable string to a mutable spannable string
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        for (CharacterStyle c : cs) ssb.setSpan(c, start, end, 0);
        text = ssb;
    }
    return text;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Aggregations

CharacterStyle (android.text.style.CharacterStyle)58 SpannableStringBuilder (android.text.SpannableStringBuilder)22 ForegroundColorSpan (android.text.style.ForegroundColorSpan)18 RelativeSizeSpan (android.text.style.RelativeSizeSpan)18 Paint (android.graphics.Paint)16 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)14 StyleSpan (android.text.style.StyleSpan)13 TextPaint (android.text.TextPaint)12 ParcelableSpan (android.text.ParcelableSpan)9 SpannableString (android.text.SpannableString)9 Spanned (android.text.Spanned)9 StrikethroughSpan (android.text.style.StrikethroughSpan)9 TypefaceSpan (android.text.style.TypefaceSpan)9 UnderlineSpan (android.text.style.UnderlineSpan)9 BackgroundColorSpan (android.text.style.BackgroundColorSpan)8 ReplacementSpan (android.text.style.ReplacementSpan)8 SubscriptSpan (android.text.style.SubscriptSpan)8 SuperscriptSpan (android.text.style.SuperscriptSpan)8 URLSpan (android.text.style.URLSpan)8 ImageSpan (android.text.style.ImageSpan)7