Search in sources :

Example 56 with CharacterStyle

use of android.text.style.CharacterStyle in project Jota-Text-Editor-old by jiro-aqua.

the class TextUtils method writeToParcel.

/**
 * Flatten a CharSequence and whatever styles can be copied across processes
 * into the parcel.
 */
public static void writeToParcel(CharSequence cs, Parcel p, int parcelableFlags) {
    if (cs instanceof Spanned) {
        p.writeInt(0);
        p.writeString(cs.toString());
        Spanned sp = (Spanned) cs;
        Object[] os = sp.getSpans(0, cs.length(), Object.class);
        for (int i = 0; i < os.length; i++) {
            Object o = os[i];
            Object prop = os[i];
            if (prop instanceof CharacterStyle) {
                prop = ((CharacterStyle) prop).getUnderlying();
            }
            if (prop instanceof ParcelableSpan) {
                ParcelableSpan ps = (ParcelableSpan) prop;
                p.writeInt(ps.getSpanTypeId());
                ps.writeToParcel(p, parcelableFlags);
                writeWhere(p, sp, o);
            }
        }
        p.writeInt(0);
    } else {
        p.writeInt(1);
        if (cs != null) {
            p.writeString(cs.toString());
        } else {
            p.writeString(null);
        }
    }
}
Also used : Spanned(android.text.Spanned) ParcelableSpan(android.text.ParcelableSpan) TextPaint(android.text.TextPaint) CharacterStyle(android.text.style.CharacterStyle)

Example 57 with CharacterStyle

use of android.text.style.CharacterStyle in project smartmodule by carozhu.

the class StringUtil method highlight.

/**
 * 关键字高亮显示
 *
 * @param target 需要高亮的关键字
 */
public static String highlight(String strold, String target) {
    String temp = strold.toString();
    SpannableStringBuilder spannable = new SpannableStringBuilder(temp);
    CharacterStyle span = null;
    Pattern p = Pattern.compile(target);
    Matcher m = p.matcher(temp);
    while (m.find()) {
        // 需要重复! //span = new
        span = new ForegroundColorSpan(Color.RED);
        // ImageSpan(drawable,ImageSpan.XX);//设置现在图片
        spannable.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spannable.toString();
}
Also used : Pattern(java.util.regex.Pattern) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Matcher(java.util.regex.Matcher) BytesUtils.bytesToHexString(com.caro.smartmodule.utils.BytesUtils.bytesToHexString) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 58 with CharacterStyle

use of android.text.style.CharacterStyle in project Signal-Android by WhisperSystems.

the class ConversationItem method getLongMessageSpan.

// / Event handlers
private Spannable getLongMessageSpan(@NonNull MessageRecord messageRecord) {
    String message;
    Runnable action;
    if (messageRecord.isMms()) {
        TextSlide slide = ((MmsMessageRecord) messageRecord).getSlideDeck().getTextSlide();
        if (slide != null && slide.asAttachment().getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_DONE) {
            message = getResources().getString(R.string.ConversationItem_read_more);
            action = () -> eventListener.onMoreTextClicked(conversationRecipient.getId(), messageRecord.getId(), messageRecord.isMms());
        } else if (slide != null && slide.asAttachment().getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_STARTED) {
            message = getResources().getString(R.string.ConversationItem_pending);
            action = () -> {
            };
        } else if (slide != null) {
            message = getResources().getString(R.string.ConversationItem_download_more);
            action = () -> singleDownloadClickListener.onClick(bodyText, slide);
        } else {
            message = getResources().getString(R.string.ConversationItem_read_more);
            action = () -> eventListener.onMoreTextClicked(conversationRecipient.getId(), messageRecord.getId(), messageRecord.isMms());
        }
    } else {
        message = getResources().getString(R.string.ConversationItem_read_more);
        action = () -> eventListener.onMoreTextClicked(conversationRecipient.getId(), messageRecord.getId(), messageRecord.isMms());
    }
    SpannableStringBuilder span = new SpannableStringBuilder(message);
    CharacterStyle style = new ClickableSpan() {

        @Override
        public void onClick(@NonNull View widget) {
            if (eventListener != null && batchSelected.isEmpty()) {
                action.run();
            }
        }

        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            ds.setTypeface(Typeface.DEFAULT_BOLD);
        }
    };
    span.setSpan(style, 0, span.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return span;
}
Also used : NonNull(androidx.annotation.NonNull) TextSlide(org.thoughtcrime.securesms.mms.TextSlide) SpannableString(android.text.SpannableString) ClickableSpan(android.text.style.ClickableSpan) LinkPreviewView(org.thoughtcrime.securesms.components.LinkPreviewView) AlertView(org.thoughtcrime.securesms.components.AlertView) ViewOnceMessageView(org.thoughtcrime.securesms.revealable.ViewOnceMessageView) PlaybackSpeedToggleTextView(org.thoughtcrime.securesms.components.PlaybackSpeedToggleTextView) AvatarImageView(org.thoughtcrime.securesms.components.AvatarImageView) DocumentView(org.thoughtcrime.securesms.components.DocumentView) EmojiTextView(org.thoughtcrime.securesms.components.emoji.EmojiTextView) View(android.view.View) QuoteView(org.thoughtcrime.securesms.components.QuoteView) SharedContactView(org.thoughtcrime.securesms.components.SharedContactView) TextView(android.widget.TextView) ReactionsConversationView(org.thoughtcrime.securesms.reactions.ReactionsConversationView) BorderlessImageView(org.thoughtcrime.securesms.components.BorderlessImageView) AudioView(org.thoughtcrime.securesms.components.AudioView) BadgeImageView(org.thoughtcrime.securesms.badges.BadgeImageView) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle) TextPaint(android.text.TextPaint)

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