Search in sources :

Example 6 with SpannedString

use of android.text.SpannedString in project KeepScore by nolanlawson.

the class AutofitTextView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = getHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    int maxNumLines = (height / getLineHeight());
    if (maxNumLines >= 2) {
        String oldText = getText().toString();
        int cutoffIndex = StringUtil.getNthIndexOf('\n', oldText, maxNumLines);
        if (cutoffIndex != -1) {
            // cut off the text
            int startOfLastLine = StringUtil.getNthIndexOf('\n', oldText, maxNumLines - 1);
            // make the last line semi-transparent, to indicate that there
            // are more results left
            // (similar to the ellipsize effect in normal text views, but
            // vertical)
            // get the original color (blue or red)
            ForegroundColorSpan[] foregroundColorSpans = ((SpannedString) getText()).getSpans(startOfLastLine + 1, cutoffIndex, ForegroundColorSpan.class);
            // make an alpha-ized gradient out of the original color
            int originalColor = foregroundColorSpans[0].getForegroundColor();
            int startColor = (START_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int endColor = (END_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int numLines = StringUtil.count(getText().subSequence(0, cutoffIndex).toString(), "\n");
            float startY = (numLines * getLineHeight());
            float endY = startY + getLineHeight();
            // build up a new spannable
            SpannableStringBuilder builder = new SpannableStringBuilder().append(getText().subSequence(0, startOfLastLine)).append(getText().subSequence(startOfLastLine, cutoffIndex).toString());
            builder.setSpan(new TopDownGradientSpan(startColor, endColor, startY, endY), startOfLastLine, cutoffIndex, 0);
            setText(builder);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) TopDownGradientSpan(com.nolanlawson.keepscore.android.TopDownGradientSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 7 with SpannedString

use of android.text.SpannedString in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SpannableStringUtils method concatWithNonParagraphSuggestionSpansOnly.

/**
     * Returns a CharSequence concatenating the specified CharSequences, retaining their
     * SuggestionSpans that don't have the PARAGRAPH flag, but not other spans.
     *
     * This code is almost entirely taken from {@link TextUtils#concat(CharSequence...)}, except
     * it calls copyNonParagraphSuggestionSpansFrom instead of {@link TextUtils#copySpansFrom}.
     */
public static CharSequence concatWithNonParagraphSuggestionSpansOnly(CharSequence... text) {
    if (text.length == 0) {
        return "";
    }
    if (text.length == 1) {
        return text[0];
    }
    boolean spanned = false;
    for (int i = 0; i < text.length; i++) {
        if (text[i] instanceof Spanned) {
            spanned = true;
            break;
        }
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length; i++) {
        sb.append(text[i]);
    }
    if (!spanned) {
        return sb.toString();
    }
    SpannableString ss = new SpannableString(sb);
    int off = 0;
    for (int i = 0; i < text.length; i++) {
        int len = text[i].length();
        if (text[i] instanceof Spanned) {
            copyNonParagraphSuggestionSpansFrom((Spanned) text[i], 0, len, ss, off);
        }
        off += len;
    }
    return new SpannedString(ss);
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned)

Example 8 with SpannedString

use of android.text.SpannedString in project android_frameworks_base by AOSPA.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 9 with SpannedString

use of android.text.SpannedString in project android_frameworks_base by DirtyUnicorns.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 10 with SpannedString

use of android.text.SpannedString in project MVPArms by JessYanCoding.

the class UiUtils method setViewHintSize.

/**
     * 设置hint大小
     *
     * @param size
     * @param v
     * @param res
     */
public static void setViewHintSize(int size, TextView v, int res) {
    SpannableString ss = new SpannableString(getResources().getString(res));
    // 新建一个属性对象,设置文字的大小
    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
    // 附加属性到文本  
    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    // 设置hint  
    // 一定要进行转换,否则属性会消失
    v.setHint(new SpannedString(ss));
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan)

Aggregations

SpannedString (android.text.SpannedString)19 Spanned (android.text.Spanned)11 SpannableStringBuilder (android.text.SpannableStringBuilder)8 SpannableString (android.text.SpannableString)7 LocaleData (libcore.icu.LocaleData)6 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 TextPaint (android.text.TextPaint)1 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 TopDownGradientSpan (com.nolanlawson.keepscore.android.TopDownGradientSpan)1 TagHandlerReddit (com.winsonchiu.reader.data.reddit.TagHandlerReddit)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1