Search in sources :

Example 6 with StyleSpan

use of android.text.style.StyleSpan in project Conversations by siacs.

the class FormFieldWrapper method createSpannableLabelString.

protected SpannableString createSpannableLabelString(String label, boolean required) {
    SpannableString spannableString = new SpannableString(label + (required ? " *" : ""));
    if (required) {
        int start = label.length();
        int end = label.length() + 2;
        spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0);
        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.accent)), start, end, 0);
    }
    return spannableString;
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan)

Example 7 with StyleSpan

use of android.text.style.StyleSpan in project platform_frameworks_base by android.

the class NotificationTests method BOLD.

static SpannableStringBuilder BOLD(CharSequence str) {
    final SpannableStringBuilder ssb = new SpannableStringBuilder(str);
    ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
    return ssb;
}
Also used : StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 8 with StyleSpan

use of android.text.style.StyleSpan in project platform_frameworks_base by android.

the class TextUtilsTest method testEllipsize.

@LargeTest
public void testEllipsize() {
    CharSequence s1 = "The quick brown fox jumps over þhe lazy dog.";
    CharSequence s2 = new Wrapper(s1);
    Spannable s3 = new SpannableString(s1);
    s3.setSpan(new StyleSpan(0), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    TextPaint p = new TextPaint();
    p.setFlags(p.getFlags() & ~p.DEV_KERN_TEXT_FLAG);
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 3; j++) {
            TextUtils.TruncateAt kind = null;
            switch(j) {
                case 0:
                    kind = TextUtils.TruncateAt.START;
                    break;
                case 1:
                    kind = TextUtils.TruncateAt.END;
                    break;
                case 2:
                    kind = TextUtils.TruncateAt.MIDDLE;
                    break;
            }
            String out1 = TextUtils.ellipsize(s1, p, i, kind).toString();
            String out2 = TextUtils.ellipsize(s2, p, i, kind).toString();
            String out3 = TextUtils.ellipsize(s3, p, i, kind).toString();
            String keep1 = TextUtils.ellipsize(s1, p, i, kind, true, null).toString();
            String keep2 = TextUtils.ellipsize(s2, p, i, kind, true, null).toString();
            String keep3 = TextUtils.ellipsize(s3, p, i, kind, true, null).toString();
            String trim1 = keep1.replace("", "");
            // Are all normal output strings identical?
            assertEquals("wid " + i + " pass " + j, out1, out2);
            assertEquals("wid " + i + " pass " + j, out2, out3);
            // Are preserved output strings identical?
            assertEquals("wid " + i + " pass " + j, keep1, keep2);
            assertEquals("wid " + i + " pass " + j, keep2, keep3);
            // Does trimming padding from preserved yield normal?
            assertEquals("wid " + i + " pass " + j, out1, trim1);
            // Did preserved output strings preserve length?
            assertEquals("wid " + i + " pass " + j, keep1.length(), s1.length());
            // Does the output string actually fit in the space?
            assertTrue("wid " + i + " pass " + j, p.measureText(out1) <= i);
            // Is the padded output the same width as trimmed output?
            assertTrue("wid " + i + " pass " + j, p.measureText(keep1) == p.measureText(out1));
        }
    }
}
Also used : StyleSpan(android.text.style.StyleSpan) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 9 with StyleSpan

use of android.text.style.StyleSpan in project iosched by google.

the class SearchSnippet method setText.

@Override
public void setText(CharSequence text, BufferType type) {
    if (!TextUtils.isEmpty(text)) {
        Matcher matcher = PATTERN_SEARCH_TERM.matcher(text);
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        List<String> hits = new ArrayList<>();
        while (matcher.find()) {
            hits.add(matcher.group(1));
        }
        for (String hit : hits) {
            int start = ssb.toString().indexOf(hit);
            int end = start + hit.length();
            ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);
            // delete the markup tokens
            ssb.delete(end - 1, end);
            ssb.delete(start, start + 1);
        }
        text = ssb;
    }
    super.setText(text, type);
}
Also used : Matcher(java.util.regex.Matcher) StyleSpan(android.text.style.StyleSpan) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 10 with StyleSpan

use of android.text.style.StyleSpan in project android-delicious by lexs.

the class TagsBinder method buildTagList.

public static Spannable buildTagList(List<String> tags) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    if (tags == null || tags.isEmpty()) {
        return builder;
    }
    for (String tag : tags) {
        int pos = builder.length();
        builder.append(tag);
        int color = generateColor(tag);
        builder.setSpan(new ForegroundColorSpan(color), pos, builder.length(), 0);
        builder.append(SEPARATOR);
        // Thin space, so we get 1.5 spaces between
        builder.append(' ');
    }
    // Remove last thin and hard space
    builder.delete(builder.length() - 2, builder.length());
    // Make the whole thing bold
    builder.setSpan(new StyleSpan(Typeface.BOLD), 0, builder.length(), 0);
    return builder;
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

StyleSpan (android.text.style.StyleSpan)145 SpannableString (android.text.SpannableString)56 SpannableStringBuilder (android.text.SpannableStringBuilder)36 RelativeSizeSpan (android.text.style.RelativeSizeSpan)32 ForegroundColorSpan (android.text.style.ForegroundColorSpan)25 View (android.view.View)18 TextView (android.widget.TextView)17 UnderlineSpan (android.text.style.UnderlineSpan)16 Spannable (android.text.Spannable)14 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)12 TypedArray (android.content.res.TypedArray)10 TextPaint (android.text.TextPaint)10 SubscriptSpan (android.text.style.SubscriptSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)10 Handler (android.os.Handler)9 Message (android.os.Message)9 URLSpan (android.text.style.URLSpan)8 JustifiedSpan (com.bluejamesbond.text.style.JustifiedSpan)8 ArticleBuilder (com.bluejamesbond.text.util.ArticleBuilder)8