List of usage examples for android.text SpannableString length
int length();
From source file:cn.dreamtobe.emoji.ellipsize.helper.SpanEllipsizeEndHelper.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static CharSequence matchMaxWidth(SpannableString targetText, TextView textView) { if (targetText.length() <= 0) { return targetText; }//from www . j a v a 2s.c o m if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { return targetText; } if (textView == null) { return targetText; } final int maxWidth = textView.getMaxWidth(); if (maxWidth <= 0 || maxWidth >= Integer.MAX_VALUE) { return targetText; } if (textView.getEllipsize() != TextUtils.TruncateAt.END) { return targetText; } //TODO Multi-lines support if (textView.getMaxLines() != 1) { return targetText; } final String maxWidthKey = getMaxWidthKey(targetText, textView); SpannableString tmpText = SPAN_MAXWIDTH_CACHE.get(maxWidthKey); if (tmpText != null) { removeClickableSpan(tmpText); return tmpText; } TextPaint textPaint = textView.getPaint(); if (textPaint == null) { return targetText; } final int totalWidth = (int) textPaint.measureText(targetText, 0, targetText.length()); if (totalWidth <= maxWidth) { return targetText; } final long startTime = System.currentTimeMillis(); // deal maxwitdh final int dotWidth = (int) textPaint.measureText("..."); tmpText = targetText; int start = 0; int end = targetText.length(); // targetX is maxWidth - "...".length int targetX = maxWidth - dotWidth; //dichotomy: get x most touch targetX int middle = targetText.length(); int x = 0; while (start <= end) { // tx = targetX, tl = targetLength // width: 0 x // length: 0 middle end // -------------|------------- middle = (start + end) / 2; int emojiDraW = 0; int emojiStrW = 0; int emojiExcursion = 1; final Object[] tmpSpans = tmpText.getSpans(0, middle, Object.class); if (tmpSpans != null) { for (Object tmpSpan : tmpSpans) { final int tmpStart = tmpText.getSpanStart(tmpSpan); final int tmpEnd = tmpText.getSpanEnd(tmpSpan); //middle in (tmpStart, tmpEnd) if (tmpStart < middle && tmpEnd > middle) { middle = tmpEnd; emojiExcursion = tmpEnd - tmpStart; } } // TextPaint#measure do not attention span, so adjust by ourselves for (Object tmpSpan : tmpSpans) { final int tmpStart = tmpText.getSpanStart(tmpSpan); final int tmpEnd = tmpText.getSpanEnd(tmpSpan); // TODO support other span if (tmpStart < middle && tmpSpan instanceof ImageSpan) { emojiDraW += ((ImageSpan) tmpSpan).getDrawable().getBounds().width(); emojiStrW += textPaint.measureText(tmpText, tmpStart, tmpEnd); } } } x = (int) textPaint.measureText(tmpText, 0, middle); x = x - emojiStrW + emojiDraW; // x = (int) (textPaint.measureText(pureStr, 0, pureStr.length()) + emojiWidth); // Log.d(TAG, String.format("targetX: %d, currentX: %d, currentLength: %d, totalLength: %d, emojiStrW[%d], emojiDraW[%d]", targetX, x, middle, targetText.length(), emojiStrW, emojiDraW)); if (x > targetX) { // width: 0 tx x // length: start tl middle end // ----|---------|------------- // TO: start | *end // ----|--------|-------------- end = middle - emojiExcursion; } else if (x < targetX) { // width: 0 x tx // length: start middle tl end // --------------|-------|------ // TO: *start | end // ---------------|------|------ start = middle + 1; } else { break; } } // adjust x larger targetX while (x > targetX && middle > 0) { x = (int) textPaint.measureText(tmpText, 0, --middle); } // adjust x middle emoji span final Object[] ajustSpans = tmpText.getSpans(0, tmpText.length(), Object.class); for (Object adjustSpan : ajustSpans) { final int adjustStart = tmpText.getSpanStart(adjustSpan); final int adjustEnd = tmpText.getSpanEnd(adjustSpan); //[adjustStart, adjustEnd) if (middle >= adjustStart && middle < adjustEnd) { middle = adjustStart - 1; break; } } // finnal middle // sub sequence [0, middle + 1) & remove [middle +1, length] spans tmpText = (SpannableString) tmpText.subSequence(0, middle + 1); // Log.d(TAG, String.format("sub Sequence[0, %d), [%s] to [%s]", middle + 1, targetText, tmpText)); // add ... final SpannableString maxWidthSS = new SpannableString(tmpText + "..."); final Object[] maxWidthSpans = tmpText.getSpans(0, tmpText.length(), Object.class); if (maxWidthSpans != null) { for (Object maxWidthSpan : maxWidthSpans) { final int mwSpanStart = tmpText.getSpanStart(maxWidthSpan); final int mwSpanEnd = tmpText.getSpanEnd(maxWidthSpan); final int mwSpanFlag = tmpText.getSpanFlags(maxWidth); maxWidthSS.setSpan(maxWidthSpan, mwSpanStart, mwSpanEnd, mwSpanFlag); } } targetText = maxWidthSS; SPAN_MAXWIDTH_CACHE.put(maxWidthKey, targetText); Log.d(TAG, String.format("deal maxWidth %d", System.currentTimeMillis() - startTime)); return targetText; }
From source file:com.nextgis.maplibui.util.ControlHelper.java
public static void highlightText(TextView textView) { final CharSequence text = textView.getText(); final SpannableString spannableString = new SpannableString(text); spannableString.setSpan(new URLSpan(""), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString, TextView.BufferType.SPANNABLE); }
From source file:nl.mpcjanssen.simpletask.util.Util.java
public static void setColor(SpannableString ss, int color) { ss.setSpan(new ForegroundColorSpan(color), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
From source file:org.kontalk.ui.ComposeMessage.java
static CharSequence applyUpdatingStyle(CharSequence text) { // we call toString() to strip any existing span SpannableString status = new SpannableString(text.toString()); status.setSpan(sUpdatingTextSpan, 0, status.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return status; }
From source file:com.vuze.android.remote.AndroidUtilsUI.java
public static void linkify(TextView tv) { tv.setMovementMethod(LinkMovementMethod.getInstance()); CharSequence t = tv.getText(); if (!(t instanceof SpannableString)) { return;//from www.j a v a2 s.c o m } SpannableString text = (SpannableString) t; int len = text.length(); int next; for (int i = 0; i < text.length(); i = next) { next = text.nextSpanTransition(i, len, URLSpan.class); URLSpan[] old = text.getSpans(i, next, URLSpan.class); for (int j = old.length - 1; j >= 0; j--) { text.removeSpan(old[j]); UrlSpan2 span2 = new UrlSpan2(old[j].getURL()); text.setSpan(span2, i, next, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
From source file:com.securecomcode.text.notifications.MessageNotifier.java
private static void appendPushNotificationState(Context context, MasterSecret masterSecret, NotificationState notificationState, Cursor cursor) { if (masterSecret != null) return;/* w w w .jav a 2s . co m*/ PushDatabase.Reader reader = null; IncomingPushMessage message; try { reader = DatabaseFactory.getPushDatabase(context).readerFor(cursor); while ((message = reader.getNext()) != null) { Recipient recipient; try { recipient = RecipientFactory.getRecipientsFromString(context, message.getSource(), false) .getPrimaryRecipient(); } catch (RecipientFormattingException e) { Log.w("MessageNotifier", e); recipient = Recipient.getUnknownRecipient(context); } Recipients recipients = RecipientFactory.getRecipientsFromMessage(context, message, false); long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients); SpannableString body = new SpannableString( context.getString(R.string.MessageNotifier_encrypted_message)); body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); notificationState .addNotification(new NotificationItem(recipient, recipients, null, threadId, body, null)); } } finally { if (reader != null) reader.close(); } }
From source file:Main.java
public static void setTitle(TextView titleTv, int titleColor, int titleTextSize, int messageColor, int messageTextSize, CharSequence title, CharSequence message) { titleTv.setMinHeight(titleTextSize * 3); if (!TextUtils.isEmpty(title) && TextUtils.isEmpty(message)) { titleTv.setTextColor(titleColor); titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize); titleTv.getPaint().setFakeBoldText(true); titleTv.setText(title);/* ww w .j a va 2 s. c o m*/ } else if (TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { titleTv.setTextColor(messageColor); titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, messageTextSize); titleTv.setText(message); } else if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { SpannableString titleSs = new SpannableString(title + "\n" + message); titleSs.setSpan(new ForegroundColorSpan(titleColor), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new AbsoluteSizeSpan(titleTextSize), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new ForegroundColorSpan(messageColor), title.length(), titleSs.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleSs.setSpan(new AbsoluteSizeSpan(messageTextSize), title.length(), titleSs.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); titleTv.setText(titleSs); titleTv.setLineSpacing(0.0f, 1.2f); } else { titleTv.setVisibility(View.GONE); } }
From source file:com.android.mail.browse.SendersView.java
public static SpannableString getFormattedToHeader() { final SpannableString formattedToHeader = new SpannableString(sToHeaderString); final CharacterStyle readStyle = CharacterStyle.wrap(sReadStyleSpan); formattedToHeader.setSpan(readStyle, 0, formattedToHeader.length(), 0); return formattedToHeader; }
From source file:com.android.mail.browse.SendersView.java
public static SpannableString getSingularDraftString(Context context) { getSenderResources(context, true /* resourceCachingRequired */); final SpannableString formattedDraftString = new SpannableString(sDraftSingularString); final CharacterStyle readStyle = CharacterStyle.wrap(sDraftsStyleSpan); formattedDraftString.setSpan(readStyle, 0, formattedDraftString.length(), 0); return formattedDraftString; }
From source file:com.tct.mail.browse.SendersView.java
public static SpannableString getFormattedToHeader() { // TS: junwei-xu 2015-03-26 EMAIL BUGFIX-959039 MOD_S final SpannableString formattedToHeader = new SpannableString( sToHeaderString == null ? "" : sToHeaderString); // TS: junwei-xu 2015-03-26 EMAIL BUGFIX-959039 MOD_E final CharacterStyle readStyle = CharacterStyle.wrap(sReadStyleSpan); formattedToHeader.setSpan(readStyle, 0, formattedToHeader.length(), 0); return formattedToHeader; }