Search in sources :

Example 26 with StyleSpan

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

the class MessageAdapter method displayTextMessage.

private void displayTextMessage(final ViewHolder viewHolder, final Message message, boolean darkBackground, int type) {
    if (viewHolder.download_button != null) {
        viewHolder.download_button.setVisibility(View.GONE);
    }
    viewHolder.image.setVisibility(View.GONE);
    viewHolder.messageBody.setVisibility(View.VISIBLE);
    viewHolder.messageBody.setIncludeFontPadding(true);
    if (message.getBody() != null) {
        final String nick = UIHelper.getMessageDisplayName(message);
        SpannableStringBuilder body = message.getMergedBody();
        boolean hasMeCommand = message.hasMeCommand();
        if (hasMeCommand) {
            body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
        }
        if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
            body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
            body.append("…");
        }
        Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
        for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
            int start = body.getSpanStart(mergeSeparator);
            int end = body.getSpanEnd(mergeSeparator);
            body.setSpan(new DividerSpan(true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        boolean startsWithQuote = handleTextQuotes(body, darkBackground);
        if (message.getType() != Message.TYPE_PRIVATE) {
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else {
            String privateMarker;
            if (message.getStatus() <= Message.STATUS_RECEIVED) {
                privateMarker = activity.getString(R.string.private_message);
            } else {
                final String to;
                if (message.getCounterpart() != null) {
                    to = message.getCounterpart().getResourcepart();
                } else {
                    to = "";
                }
                privateMarker = activity.getString(R.string.private_message_to, to);
            }
            body.insert(0, privateMarker);
            int privateMarkerIndex = privateMarker.length();
            if (startsWithQuote) {
                body.insert(privateMarkerIndex, "\n\n");
                body.setSpan(new DividerSpan(false), privateMarkerIndex, privateMarkerIndex + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                body.insert(privateMarkerIndex, " ");
            }
            body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (hasMeCommand) {
                body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1, privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) {
            Pattern pattern = NotificationService.generateNickHighlightPattern(message.getConversation().getMucOptions().getActualNick());
            Matcher matcher = pattern.matcher(body);
            while (matcher.find()) {
                body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        Linkify.addLinks(body, XMPP_PATTERN, "xmpp");
        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
        Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
        viewHolder.messageBody.setAutoLinkMask(0);
        viewHolder.messageBody.setText(body);
        viewHolder.messageBody.setTextIsSelectable(true);
        viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
        listSelectionManager.onUpdate(viewHolder.messageBody, message);
    } else {
        viewHolder.messageBody.setText("");
        viewHolder.messageBody.setTextIsSelectable(false);
    }
    viewHolder.messageBody.setTextColor(this.getMessageTextColor(darkBackground, true));
    viewHolder.messageBody.setLinkTextColor(this.getMessageTextColor(darkBackground, true));
    viewHolder.messageBody.setHighlightColor(activity.getResources().getColor(darkBackground ? (type == SENT || !mUseGreenBackground ? R.color.black26 : R.color.grey800) : R.color.grey500));
    viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
}
Also used : Pattern(java.util.regex.Pattern) Message(eu.siacs.conversations.entities.Message) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) DividerSpan(eu.siacs.conversations.ui.text.DividerSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 27 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 28 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 29 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 30 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)143 SpannableString (android.text.SpannableString)56 SpannableStringBuilder (android.text.SpannableStringBuilder)34 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