android.text.style.StyleSpan Java Examples
The following examples show how to use
android.text.style.StyleSpan.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java From android-utils with MIT License | 6 votes |
/** * Typefaces the string as bold. * If sub-string is null, entire string will be typefaced as bold and returned. * * @param string * @param subString The subString within the string to bold. Pass null to bold entire string. * @return {@link android.text.SpannableString} */ public static SpannableStringBuilder toBold(String string, String subString) { if (TextUtils.isEmpty(string)) { return new SpannableStringBuilder(""); } SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(string); StyleSpan bss = new StyleSpan(Typeface.BOLD); if (subString != null) { int substringNameStart = string.toLowerCase().indexOf(subString); if (substringNameStart > -1) { spannableBuilder.setSpan(bss, substringNameStart, substringNameStart + subString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } else { // set entire text to bold spannableBuilder.setSpan(bss, 0, spannableBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); } return spannableBuilder; }
Example #2
Source File: MessageFormatter.java From talk-android with MIT License | 6 votes |
public static Spannable formatHighlightSpan(String html, Resources res) { HighlightSpan highlightSpan; if (Html.fromHtml(html) instanceof SpannableStringBuilder) { SpannableStringBuilder value = (SpannableStringBuilder) Html.fromHtml(html); StyleSpan[] spans = value.getSpans(0, html.length(), StyleSpan.class); for (StyleSpan span : spans) { int start = value.getSpanStart(span); int end = value.getSpanEnd(span); value.removeSpan(span); highlightSpan = new HighlightSpan(res); value.setSpan(highlightSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return value; } else { return new SpannableStringBuilder(html); } }
Example #3
Source File: StaticKeyMifareReadStep.java From Walrus with GNU General Public License v3.0 | 6 votes |
public SpannableStringBuilder getDescription(Context context) { SpannableStringBuilder builder = new SpannableStringBuilder(); // TODO XXX: i18n (w/ proper pluralisation) MiscUtils.appendAndSetSpan(builder, "Block(s): ", new StyleSpan(android.graphics.Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append(MiscUtils.unparseIntRanges(blockNumbers)); builder.append('\n'); MiscUtils.appendAndSetSpan(builder, "Key: ", new StyleSpan(android.graphics.Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append(key.toString()); builder.append('\n'); MiscUtils.appendAndSetSpan(builder, "Slot(s): ", new StyleSpan(android.graphics.Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append( keySlotAttempts == KeySlotAttempts.BOTH ? context.getString(R.string.both) : keySlotAttempts.toString()); return builder; }
Example #4
Source File: TextHelper.java From FirebaseUI-Android with Apache License 2.0 | 6 votes |
public static void boldAllOccurencesOfText(@NonNull SpannableStringBuilder builder, @NonNull String text, @NonNull String textToBold) { int fromIndex = 0; while (fromIndex < text.length()) { int start = text.indexOf(textToBold, fromIndex); int end = start + textToBold.length(); if (start == -1 || end >= text.length()) { break; } builder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); fromIndex = end + 1; } }
Example #5
Source File: KnifeText.java From Knife with Apache License 2.0 | 6 votes |
protected void styleValid(int style, int start, int end) { switch (style) { case Typeface.NORMAL: case Typeface.BOLD: case Typeface.ITALIC: case Typeface.BOLD_ITALIC: break; default: return; } if (start >= end) { return; } getEditableText().setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }
Example #6
Source File: Html.java From MHViewer with Apache License 2.0 | 6 votes |
private static void endHeader(SpannableStringBuilder text) { int len = text.length(); Object obj = getLast(text, Header.class); int where = text.getSpanStart(obj); text.removeSpan(obj); // Back off not to change only the text, not the blank line. while (len > where && text.charAt(len - 1) == '\n') { len--; } if (where != len) { Header h = (Header) obj; text.setSpan(new RelativeSizeSpan(HEADER_SIZES[h.mLevel]), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new StyleSpan(Typeface.BOLD), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
Example #7
Source File: MessageAdapter.java From Conversations with GNU General Public License v3.0 | 6 votes |
private void toggleWhisperInfo(ViewHolder viewHolder, final Message message, final boolean darkBackground) { if (message.isPrivateMessage()) { final String privateMarker; if (message.getStatus() <= Message.STATUS_RECEIVED) { privateMarker = activity.getString(R.string.private_message); } else { Jid cp = message.getCounterpart(); privateMarker = activity.getString(R.string.private_message_to, Strings.nullToEmpty(cp == null ? null : cp.getResource())); } final SpannableString body = new SpannableString(privateMarker); body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new StyleSpan(Typeface.BOLD), 0, privateMarker.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); viewHolder.messageBody.setText(body); viewHolder.messageBody.setVisibility(View.VISIBLE); } else { viewHolder.messageBody.setVisibility(View.GONE); } }
Example #8
Source File: FilesDownloadFragment.java From mcumgr-android with Apache License 2.0 | 6 votes |
private void printError(@Nullable final String error) { mDivider.setVisibility(View.VISIBLE); mResult.setVisibility(View.VISIBLE); if (error != null) { final SpannableString spannable = new SpannableString(error); spannable.setSpan(new ForegroundColorSpan( ContextCompat.getColor(requireContext(), R.color.colorError)), 0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); mResult.setText(spannable); } else { mResult.setText(null); } }
Example #9
Source File: CompetitionDetailsHeaderView.java From 1Rramp-Android with MIT License | 6 votes |
private void setParticipationHashtagInfo(String hashtag) { hashtag = "#" + hashtag; String part1 = "Participate using "; String part3 = " from any other steem platform."; int hashtagLen = hashtag.length(); int part1Len = part1.length(); int spanStart = part1Len; int spanEnd = part1Len + hashtagLen; Spannable wordtoSpan = new SpannableString(part1 + hashtag + part3); wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#3F72AF")), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); StyleSpan bss = new StyleSpan(Typeface.BOLD); wordtoSpan.setSpan(bss, spanStart, spanEnd, Spannable.SPAN_INCLUSIVE_INCLUSIVE); participationHashtagText.setText(wordtoSpan); }
Example #10
Source File: PostItemHeaderView.java From redgram-for-reddit with GNU General Public License v3.0 | 6 votes |
private void setupInfo(PostItem item) { String subreddit = "/r/"+item.getSubreddit(); CustomClickable subredditClickable = new CustomClickable(this, true); StringUtils.SpannableBuilder builder = StringUtils.newSpannableBuilder(getContext()) .setTextView(headerTimeSubredditView) .append("submitted " + item.getTime() + " hrs ago to ") .append(subreddit, subredditClickable, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) .span(new ForegroundColorSpan(Color.rgb(204, 0, 0)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) .append(" by "); if(item.distinguished() != null){ final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); builder.append(item.getAuthor(), new CustomClickable(this, false), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) .span(new ForegroundColorSpan(getAuthorBackgroundColor(item)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) .span(bss, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); }else{ builder.append(item.getAuthor(), new CustomClickable(this, true), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) .span(new ForegroundColorSpan(Color.rgb(204, 0, 0)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } builder.clickable().buildSpannable(); }
Example #11
Source File: PostItem.java From Dashchan with Apache License 2.0 | 6 votes |
public CharSequence getComment(String repliesToPost) { SpannableString comment = new SpannableString(getComment()); LinkSpan[] spans = comment.getSpans(0, comment.length(), LinkSpan.class); if (spans != null) { String commentString = comment.toString(); repliesToPost = ">>" + repliesToPost; for (LinkSpan linkSpan : spans) { int start = comment.getSpanStart(linkSpan); if (commentString.indexOf(repliesToPost, start) == start) { int end = comment.getSpanEnd(linkSpan); comment.setSpan(new StyleSpan(Typeface.BOLD), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return comment; }
Example #12
Source File: FormatUtils.java From Gander with Apache License 2.0 | 6 votes |
public static CharSequence formatFormEncoded(String formEncoded) { try { Truss truss = new Truss(); if (formEncoded != null) { formEncoded = URLDecoder.decode(formEncoded, "UTF-8"); String[] pairs = formEncoded.split("&"); for (String pair : pairs) { if (pair.contains("=")) { int idx = pair.indexOf("="); truss.pushSpan(new StyleSpan(android.graphics.Typeface.BOLD)); truss.append(pair.substring(0, idx)).append("= "); truss.popSpan(); truss.append(pair.substring(idx + 1)).append("\n"); } } } return truss.build(); } catch (Exception e) { Logger.e("non form url content content", e); return formEncoded; } }
Example #13
Source File: ChatEntry.java From AndFChat with GNU General Public License v3.0 | 6 votes |
public Spannable getChatMessage(Context context) { if (spannedText == null) { Spannable dateSpan = createDateSpannable(context); Spannable textSpan = createText(context); // Time SpannableStringBuilder finishedText = new SpannableStringBuilder(dateSpan); // Delimiter finishedText.append(delimiterBetweenDateAndName); // Name finishedText.append(new NameSpannable(owner, getNameColorId(), context.getResources())); // Delimiter finishedText.append(delimiterBetweenNameAndText); // Message finishedText.append(textSpan); // Add overall styles if (getTypeFace() != null) { finishedText.setSpan(new StyleSpan(getTypeFace()), DATE_CHAR_LENGTH, finishedText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); } spannedText = finishedText; } return spannedText; }
Example #14
Source File: HtmlParser.java From Overchan-Android with GNU General Public License v3.0 | 6 votes |
public HtmlToSpannedConverter(String subject, String source, ThemeColors colors, HtmlParser.ImageGetter imageGetter, boolean openSpoilers, Parser parser) { mSource = source; mSpannableStringBuilder = new SpannableStringBuilder(); if (!TextUtils.isEmpty(subject)) { mSpannableStringBuilder.append(subject); int len = mSpannableStringBuilder.length(); mSpannableStringBuilder.setSpan(new RelativeSizeSpan(1.25f), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mSpannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); if (colors != null) { mSpannableStringBuilder.setSpan(new ForegroundColorSpan(colors.subjectForeground), 0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } mSpannableStringBuilder.append('\n'); mStartLength = mSpannableStringBuilder.length(); } mColors = colors; mOpenSpoilers = openSpoilers; mImageGetter = imageGetter; mReader = parser; }
Example #15
Source File: SearchScreenOverlay.java From talkback with Apache License 2.0 | 6 votes |
/** * Makes all instances of {@code keyword} within {@code fullString} bold. * * @param matchedInfo the matchedNodeInfo containing the matched {@code AccessibilityNode} and * {@code MatchResult} * @return the fullString with all contained keyword set as bold or null if node is not available */ static SpannableStringBuilder makeAllKeywordBold(@Nullable MatchedNodeInfo matchedInfo) { if (matchedInfo == null || TextUtils.isEmpty(matchedInfo.getNodeText())) { return null; } SpannableStringBuilder updatedString = new SpannableStringBuilder(matchedInfo.getNodeText()); if (!matchedInfo.hasMatchedResult()) { return updatedString; } // Clear existing formatting spans since we only want the highlight span shown in the result // list. clearFormattingSpans(updatedString); for (MatchResult match : matchedInfo.matchResults()) { updatedString.setSpan( new StyleSpan(Typeface.BOLD), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return updatedString; }
Example #16
Source File: CameraActivity.java From dbclf with Apache License 2.0 | 5 votes |
private SpannableString generateCenterSpannableText() { final SpannableString s = new SpannableString("Center dog here\nkeep camera stable"); s.setSpan(new RelativeSizeSpan(1.5f), 0, 15, 0); s.setSpan(new StyleSpan(Typeface.NORMAL), 15, s.length() - 15, 0); s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 0, 15, 0); s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 18, s.length(), 0); s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 18, s.length(), 0); return s; }
Example #17
Source File: ForbidListAdapter.java From tysq-android with GNU General Public License v3.0 | 5 votes |
/** * 对用户名进行加粗并进行组装内容 * @param accountName * @return */ private SpannableStringBuilder setAccountName(String accountName){ SpannableStringBuilder spannableString = new SpannableStringBuilder(accountName); //设置字体粗细 StyleSpan span = new StyleSpan(Typeface.BOLD); spannableString.setSpan(span, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; }
Example #18
Source File: DiscussionTextUtils.java From edx-app-android with Apache License 2.0 | 5 votes |
public static void setAuthorText(@NonNull TextView textView, @NonNull IAuthorData authorData) { final CharSequence authorText; { final Context context = textView.getContext(); List<CharSequence> joinableStrings = new ArrayList<>(); final String author = authorData.getAuthor(); if (!TextUtils.isEmpty(author)) { final SpannableString authorSpan = new SpannableString(author); // Change the author text color and style if (!authorData.isAuthorAnonymous()) { authorSpan.setSpan(new ForegroundColorSpan(context.getResources(). getColor(R.color.edx_brand_primary_base)), 0, authorSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } authorSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, authorSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); joinableStrings.add(authorSpan); } final String authorLabel = authorData.getAuthorLabel(); if (!TextUtils.isEmpty(authorLabel)) { joinableStrings.add(ResourceUtil.getFormattedString(context.getResources(), R.string.discussion_post_author_label_attribution, "text", authorLabel)); } authorText = org.edx.mobile.util.TextUtils.join(" ", joinableStrings); } if (TextUtils.isEmpty(authorText)) { textView.setVisibility(View.GONE); } else { textView.setText(authorText); } }
Example #19
Source File: SearchActivity.java From materialup with Apache License 2.0 | 5 votes |
private void setNoResultsVisibility(int visibility) { if (visibility == View.VISIBLE) { if (noResults == null) { noResults = (BaselineGridTextView) ((ViewStub) findViewById(R.id.stub_no_search_results)).inflate(); noResults.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { searchView.setQuery("", false); searchView.requestFocus(); ImeUtils.showIme(searchView); } }); } String message = String.format(getString(R .string.no_search_results), searchView.getQuery().toString()); SpannableStringBuilder ssb = new SpannableStringBuilder(message); ssb.setSpan(new StyleSpan(Typeface.ITALIC), message.indexOf('“') + 1, message.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); noResults.setText(ssb); } if (noResults != null) { noResults.setVisibility(visibility); } }
Example #20
Source File: SpanUtils.java From styT with Apache License 2.0 | 5 votes |
public static SpannableString getStyleSpan() { SpannableString spannableString = new SpannableString("身正不怕影子歪"); StyleSpan styleSpanBold = new StyleSpan(Typeface.BOLD); StyleSpan styleSpanitalic = new StyleSpan(Typeface.ITALIC); spannableString.setSpan(styleSpanBold, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE); spannableString.setSpan(styleSpanitalic, 4, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; }
Example #21
Source File: ParticipateEditorActivity.java From 1Rramp-Android with MIT License | 5 votes |
private void setContestInfo() { String comp_title_part1 = "Your submission for: "; String comp_title_part2 = mCompetitionTitle; int spanStart = comp_title_part1.length(); int spanEnd = comp_title_part2.length() + spanStart; StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#3F72AF")); Spannable compTitleSpan = new SpannableString(String.format("%s%s", comp_title_part1, comp_title_part2)); //color span compTitleSpan.setSpan(foregroundColorSpan, spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold span compTitleSpan.setSpan(bss, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); String hashtag_part1 = "#" + mCompetitionHashtag; String hashtag_part2 = " is auto tagged into your submission.(No need to write it again)"; spanStart = 0; spanEnd = hashtag_part1.length(); Spannable compHashtagSpan = new SpannableString(String.format("%s%s", hashtag_part1, hashtag_part2)); //color span compHashtagSpan.setSpan(foregroundColorSpan, spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold span compHashtagSpan.setSpan(bss, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE); submissionInfo.setText(compTitleSpan); autoHashtagsText.setText(compHashtagSpan); }
Example #22
Source File: BrowserWindow.java From Beedio with GNU General Public License v2.0 | 5 votes |
public void updateNumWindows(int num) { final String numWindowsString = "Windows[" + num + "]"; final SpannableStringBuilder sb = new SpannableStringBuilder(numWindowsString); final ForegroundColorSpan fcs = new ForegroundColorSpan(LMvdApp.getInstance() .getApplicationContext().getResources().getColor(R.color.darkColor)); final StyleSpan bss = new StyleSpan(Typeface.BOLD); sb.setSpan(fcs, 8, 8 + String.valueOf(num).length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); sb.setSpan(bss, 8, 8 + String.valueOf(num).length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { numWindows.setText(sb); } }); }
Example #23
Source File: LanguageOptionsView.java From FirefoxReality with Mozilla Public License 2.0 | 5 votes |
private SpannableStringBuilder getSpannedLanguageText(@NonNull String language) { int end = getLanguageIndex(language); SpannableStringBuilder spanned = new SpannableStringBuilder(language); if (end > 0) { spanned.setSpan(new StyleSpan(Typeface.BOLD), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return spanned; }
Example #24
Source File: AndroidNotifications.java From actor-platform with GNU Affero General Public License v3.0 | 5 votes |
private CharSequence getNotificationTextFull(Notification notification, Messenger messenger) { SpannableStringBuilder res = new SpannableStringBuilder(); if (!messenger.getFormatter().isLargeDialogMessage(notification.getContentDescription().getContentType())) { res.append(getNotificationSender(notification)); res.append(": "); res.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, res.length(), 0); } res.append(messenger.getFormatter().formatNotificationText(notification)); return res; }
Example #25
Source File: LogbookActivity.java From homeassist with Apache License 2.0 | 5 votes |
@Override public void onBindViewHolder(final LogbookViewHolder viewHolder, final int position) { final LogSheet logSheet = items.get(position); Log.d("YouQi", "rendering: " + position + "logSheet.message: " + logSheet.message); //viewHolder.mNameView.setText(logSheet.name); viewHolder.mStateText.setText(TextUtils.concat(dateFormat.format(logSheet.when.getTime()), "\n", CommonUtil.getSpanText(LogbookActivity.this, DateUtils.getRelativeTimeSpanString(logSheet.when.getTime()).toString(), null, 0.9f))); viewHolder.mIconView.setText(MDIFont.getIcon("mdi:information-outline")); Entity entity = getEntity(logSheet.entityId); if (entity != null) { viewHolder.mIconView.setText(entity.getMdiIcon()); } Spannable wordtoSpan1 = new SpannableString(logSheet.name); wordtoSpan1.setSpan(new RelativeSizeSpan(1.0f), 0, wordtoSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordtoSpan1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, wordtoSpan1.length(), 0); Spannable wordtoSpan2 = new SpannableString(logSheet.message); wordtoSpan2.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.md_grey_500, null)), 0, wordtoSpan2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordtoSpan2.setSpan(new RelativeSizeSpan(0.95f), 0, wordtoSpan2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //viewHolder.mNameView.setText(TextUtils.concat(wordtoSpan1, " ", wordtoSpan2)); viewHolder.mNameView.setText(logSheet.name); viewHolder.mSubText.setText(logSheet.message); }
Example #26
Source File: StarActivity.java From letv with Apache License 2.0 | 5 votes |
private void drawStarVoteNum(String num, String unit) { if (!TextUtils.isEmpty(num) && !TextUtils.isEmpty(unit)) { String str = num + " " + unit; int start = num.length(); int end = str.length(); SpannableStringBuilder sb = new SpannableStringBuilder(str); sb.setSpan(new ForegroundColorSpan(getResources().getColor(2131493270)), start, end, 33); sb.setSpan(new StyleSpan(1), 0, start, 33); sb.setSpan(new AbsoluteSizeSpan(getResources().getDimensionPixelSize(2131165477)), start, end, 33); this.mStarRankVoteNum.setText(sb); } }
Example #27
Source File: KnifeText.java From Knife with Apache License 2.0 | 5 votes |
protected void styleInvalid(int style, int start, int end) { switch (style) { case Typeface.NORMAL: case Typeface.BOLD: case Typeface.ITALIC: case Typeface.BOLD_ITALIC: break; default: return; } if (start >= end) { return; } StyleSpan[] spans = getEditableText().getSpans(start, end, StyleSpan.class); List<KnifePart> list = new ArrayList<>(); for (StyleSpan span : spans) { if (span.getStyle() == style) { list.add(new KnifePart(getEditableText().getSpanStart(span), getEditableText().getSpanEnd(span))); getEditableText().removeSpan(span); } } for (KnifePart part : list) { if (part.isValid()) { if (part.getStart() < start) { styleValid(style, part.getStart(), start); } if (part.getEnd() > end) { styleValid(style, end, part.getEnd()); } } } }
Example #28
Source File: TextFormatBar.java From revolution-irc with GNU General Public License v3.0 | 5 votes |
public void updateFormattingAtCursor() { if (mEditText == null) return; Editable text = mEditText.getText(); int start = mEditText.getSelectionStart(); int end = mEditText.getSelectionEnd(); Object[] spans = text.getSpans(start, end, Object.class); mBoldButton.setSelected(false); mItalicButton.setSelected(false); mUnderlineButton.setSelected(false); int fgColor = -1; int bgColor = -1; for (Object span : spans) { if (!SpannableStringHelper.checkSpanInclude(text, span, start, end) || (text.getSpanFlags(span) & Spanned.SPAN_COMPOSING) != 0) continue; if (span instanceof StyleSpan) { int style = ((StyleSpan) span).getStyle(); if (style == Typeface.BOLD) mBoldButton.setSelected(true); else if (style == Typeface.ITALIC) mItalicButton.setSelected(true); } else if (span instanceof UnderlineSpan) { mUnderlineButton.setSelected(true); } else if (span instanceof ForegroundColorSpan) { fgColor = ((ForegroundColorSpan) span).getForegroundColor(); } else if (span instanceof BackgroundColorSpan) { bgColor = ((BackgroundColorSpan) span).getBackgroundColor(); } } ImageViewCompat.setImageTintList(mTextColorValue, fgColor != -1 ? ColorStateList.valueOf(fgColor) : mTextColorValueDefault); ImageViewCompat.setImageTintList(mFillColorValue, bgColor != -1 ? ColorStateList.valueOf(bgColor) : mFillColorValueDefault); }
Example #29
Source File: UtilitiesTest.java From google-authenticator-android with Apache License 2.0 | 5 votes |
@Test public void testGetStyledTextFromHtmlWithInputWithoutParagraphs() { Spanned result = Utilities.getStyledTextFromHtml("123<b>first</b>, <i>second</i>"); assertThat(result.toString()).isEqualTo("123first, second"); StyleSpan[] spans = result.getSpans(0, result.length(), StyleSpan.class); assertThat(spans).hasLength(2); assertSpanLocation(spans[0], result, 3, 8); assertSpanLocation(spans[1], result, 10, 16); }
Example #30
Source File: StatsFragment.java From mcumgr-android with Apache License 2.0 | 5 votes |
private void printStats(@NonNull final List<McuMgrStatResponse> responses) { final SpannableStringBuilder builder = new SpannableStringBuilder(); for (final McuMgrStatResponse response : responses) { final int start = builder.length(); builder.append(getString(R.string.stats_module, response.name)).append("\n"); builder.setSpan(new StyleSpan(Typeface.BOLD), start, start + response.name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); for (final Map.Entry<String, Long> entry : response.fields.entrySet()) { builder.append(getString(R.string.stats_field, entry.getKey(), entry.getValue())).append("\n"); } } mStatsValue.setText(builder); }