List of usage examples for android.text SpannableString SpannableString
public SpannableString(CharSequence source)
From source file:Main.java
/** * Returns a CharSequence containing a bulleted and properly indented list. * * @param leadingMargin/*from w w w.j a va 2 s . co m*/ * In pixels, the space between the left edge of the bullet and * the left edge of the text. * @param lines * An array of CharSequences. Each CharSequences will be a * separate line/bullet-point. * @return */ public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) { SpannableStringBuilder sb = new SpannableStringBuilder(); for (int i = 0; i < lines.length; i++) { CharSequence line = lines[i] + (i < lines.length - 1 ? "\n" : ""); Spannable spannable = new SpannableString(line); spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); sb.append(spannable); } return sb; }
From source file:Main.java
public static CharSequence getBoldedString(String value) { SpannableString spanned = new SpannableString(value); spanned.setSpan(new StyleSpan(Typeface.BOLD), 0, spanned.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spanned; }
From source file:Main.java
public static SpannableString colourize(String raw) { String uncoloredName = uncolourize(raw); SpannableString str = new SpannableString(uncoloredName); char c, cc;/* w w w . j a va 2 s . co m*/ String color = "RESETT"; int start = 0; int end = 0; for (int i = 0; i < raw.length() - 1; i++) { c = raw.charAt(i); cc = raw.charAt(i + 1); // if we detect a new color tag if (c == '0' && cc == 'x') { if (start != end) { str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } start = end; i += 2; color = ""; for (int k = i; k < i + 6; k++) { try { color += raw.charAt(k); } catch (IndexOutOfBoundsException e) { color = ""; break; } } i += 5; } else { end++; } } str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, uncoloredName.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); return str; }
From source file:Main.java
public static SpannableString textSpannable(String text) { SpannableString s;/*from ww w .j av a 2 s .c o m*/ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { s = new SpannableString(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)); } else { //noinspection deprecation s = new SpannableString(Html.fromHtml(text)); } Linkify.addLinks(s, Linkify.WEB_URLS); return s; }
From source file:Main.java
/** * Create a colored string./*from www. j av a2 s . c om*/ * * @param data The text to be colored. * @param color The color for the text. * @return A colored string. */ public static SpannableString colorString(String data, int color) { SpannableString ret = new SpannableString(data); ret.setSpan(new ForegroundColorSpan(color), 0, data.length(), 0); return ret; }
From source file:Main.java
/** * Returns a CharSequence concatenating the specified CharSequences using the specified delimiter, * retaining their spans if any.// ww w . j ava 2 s . co m * * This is mostly borrowed from TextUtils.concat(); */ public static CharSequence joinSpannables(String delimiter, CharSequence... text) { if (text.length == 0) { return ""; } if (text.length == 1) { return text[0]; } boolean spanned = false; for (int i = 0; i < text.length; i++) { if (text[i] instanceof Spanned) { spanned = true; break; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length; i++) { if (i > 0) { sb.append(delimiter); } sb.append(text[i]); } if (!spanned) { return sb.toString(); } SpannableString ss = new SpannableString(sb); int off = 0; for (int i = 0; i < text.length; i++) { int len = text[i].length(); if (text[i] instanceof Spanned) { TextUtils.copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off); } off += len + delimiter.length(); } return new SpannedString(ss); }
From source file:Main.java
public static CharSequence addIcon(CharSequence total, BitmapDrawable bitmapDrawable, int height) { SpannableString string = new SpannableString(" "); ImageSpan imageSpan = new ImageSpan(bitmapDrawable); int width = (int) (height / (bitmapDrawable.getIntrinsicHeight() / (float) bitmapDrawable.getIntrinsicWidth())); imageSpan.getDrawable().setBounds(0, 0, width, height); string.setSpan(imageSpan, 0, 1, 0);//from w w w .j ava2 s. c o m if (total == null) { return string; } else { return TextUtils.concat(total, string); } }
From source file:Main.java
public static CharSequence handleAcUrl(CharSequence content) { Matcher m = AC_PATTERN.matcher(content); Spannable spannable = null;//from w w w . j a v a2s. com while (m.find()) { // Ensure spannable if (spannable == null) { if (content instanceof Spannable) { spannable = (Spannable) content; } else { spannable = new SpannableString(content); } } int start = m.start(); int end = m.end(); URLSpan[] links = spannable.getSpans(start, end, URLSpan.class); if (links.length > 0) { // There has been URLSpan already, leave it alone continue; } URLSpan urlSpan = new URLSpan("http://www.acfun.tv/v/" + m.group(0)); spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return spannable == null ? content : spannable; }
From source file:Main.java
public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern, Drawable drawable) {/*from w w w. j a va2 s . c o m*/ SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence : new SpannableString(charSequence); try { ImageSpan is = new ImageSpan(drawable); Pattern pattern = Pattern.compile(regPattern); Matcher matcher = pattern.matcher(ss); while (matcher.find()) { String key = matcher.group(); ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } } catch (Exception ex) { ex.printStackTrace(); } return ss; }
From source file:Main.java
public static SpannableString getSmallCapsString(String input) { // values needed to record start/end points of blocks of lowercase letters char[] chars = input.toCharArray(); int currentBlock = 0; int[] blockStarts = new int[chars.length]; int[] blockEnds = new int[chars.length]; boolean blockOpen = false; // record where blocks of lowercase letters start/end for (int i = 0; i < chars.length; ++i) { char c = chars[i]; if (c >= 'a' && c <= 'z') { if (!blockOpen) { blockOpen = true;//from www.j av a 2s.co m blockStarts[currentBlock] = i; } // replace with uppercase letters chars[i] = (char) (c - 'a' + '\u0041'); } else { if (blockOpen) { blockOpen = false; blockEnds[currentBlock] = i; ++currentBlock; } } } // add the string end, in case the last character is a lowercase letter blockEnds[currentBlock] = chars.length; // shrink the blocks found above SpannableString output = new SpannableString(String.valueOf(chars)); for (int i = 0; i < Math.min(blockStarts.length, blockEnds.length); ++i) { output.setSpan(new RelativeSizeSpan(0.8f), blockStarts[i], blockEnds[i], Spannable.SPAN_EXCLUSIVE_INCLUSIVE); } return output; }