Java Code Examples for android.text.SpannableString#removeSpan()

The following examples show how to use android.text.SpannableString#removeSpan() . 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: LinkTransformationMethod.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
            return source;
        }
        SpannableString text = (SpannableString) textView.getText();
        URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
        for (int i = spans.length - 1; i >= 0; i--) {
            URLSpan oldSpan = spans[i];
            int start = text.getSpanStart(oldSpan);
            int end = text.getSpanEnd(oldSpan);
            String url = oldSpan.getURL();
            text.removeSpan(oldSpan);
            text.setSpan(new CustomUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return text;
    }
    return source;
}
 
Example 2
Source File: LinkTransformationMethod.java    From nano-wallet-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
         if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
            return source;
        }
        SpannableString text = (SpannableString) textView.getText();
        URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
        for (int i = spans.length - 1; i >= 0; i--) {
            URLSpan oldSpan = spans[i];
            int start = text.getSpanStart(oldSpan);
            int end = text.getSpanEnd(oldSpan);
            String url = oldSpan.getURL();
            text.removeSpan(oldSpan);
            text.setSpan(new CustomUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return text;
    }
    return source;
}
 
Example 3
Source File: LinkifyHelper.java    From Zom-Android-XMPP with GNU General Public License v3.0 6 votes vote down vote up
public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll(
        CharSequence original, Class<A> sourceType, SpanConverter<A, B> converter) {
    SpannableString result = new SpannableString(original);
    A[] spans = result.getSpans(0, result.length(), sourceType);

    for (A span : spans) {
        int start = result.getSpanStart(span);
        int end = result.getSpanEnd(span);
        int flags = result.getSpanFlags(span);

        result.removeSpan(span);
        result.setSpan(converter.convert(span), start, end, flags);
    }

    return (result);
}
 
Example 4
Source File: TextEventInterpreter.java    From talkback with Apache License 2.0 6 votes vote down vote up
@Nullable
public static CharSequence getSubsequenceWithSpans(
    @Nullable CharSequence text, int from, int to) {
  if (text == null) {
    return null;
  }
  if (from < 0 || text.length() < to || to < from) {
    return null;
  }

  SpannableString textWithSpans = SpannableString.valueOf(text);
  CharSequence subsequence = text.subSequence(from, to);
  SpannableString subsequenceWithSpans = SpannableString.valueOf(subsequence);
  TtsSpan[] spans = subsequenceWithSpans.getSpans(0, subsequence.length(), TtsSpan.class);

  for (TtsSpan span : spans) {
    if (textWithSpans.getSpanStart(span) < from || to < textWithSpans.getSpanEnd(span)) {
      subsequenceWithSpans.removeSpan(span);
    }
  }
  return subsequence;
}
 
Example 5
Source File: SmallAnimatedTextView.java    From tilt-game-android with MIT License 6 votes vote down vote up
@Override
public void setText(CharSequence text, BufferType type) {
	_newText = new SpannableString(text);
	WordSpan[] spans = _newText.getSpans(0, text.length(), WordSpan.class);

	for (WordSpan span : spans) {
		_newText.removeSpan(span);
	}

	String[] words = text.toString().split(" ");
	int charIndex = 0;
	for (int i = 0; i < words.length; i++) {
		int endWordIndex = charIndex + words[i].length();
		_newText.setSpan(new WordSpan(getTextSize()), charIndex, endWordIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		charIndex = endWordIndex + 1;
	}

	super.setText(_newText, BufferType.SPANNABLE);
}
 
Example 6
Source File: Utils.java    From openshop.io-android with MIT License 5 votes vote down vote up
/**
 * Method replace ordinary {@link URLSpan} with {@link DefensiveURLSpan}.
 *
 * @param spannedText text, where link spans should be replaced.
 * @param activity    activity for displaying problems.
 * @return text, where link spans are replaced.
 */
public static SpannableString safeURLSpanLinks(Spanned spannedText, Activity activity) {
    final SpannableString current = new SpannableString(spannedText);
    final URLSpan[] spans = current.getSpans(0, current.length(), URLSpan.class);
    int start, end;

    for (URLSpan span : spans) {
        start = current.getSpanStart(span);
        end = current.getSpanEnd(span);
        current.removeSpan(span);
        current.setSpan(new DefensiveURLSpan(span.getURL(), activity), start, end, 0);
    }
    return current;
}
 
Example 7
Source File: SpanFormatter.java    From QuickDevFramework with Apache License 2.0 5 votes vote down vote up
private static CharSequence fixSpanColor(CharSequence text) {
	if (text instanceof Spanned) {
		final SpannableString s = new SpannableString(text);
		final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
		for (final ForegroundColorSpan oldSpan : spans) {
			final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000);
			s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan));
			s.removeSpan(oldSpan);
		}
		return s;
	} else {
		return text;
	}
}
 
Example 8
Source File: SpannableUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Strip out all the spans of target span class from the given text.
 *
 * @param text Text to remove span.
 * @param spanClass class of span to be removed.
 */
public static <T> void stripTargetSpanFromText(CharSequence text, Class<T> spanClass) {
  if (TextUtils.isEmpty(text) || !(text instanceof SpannableString)) {
    return;
  }
  SpannableString spannable = (SpannableString) text;
  T[] spans = spannable.getSpans(0, spannable.length(), spanClass);
  if (spans != null) {
    for (T span : spans) {
      if (span != null) {
        spannable.removeSpan(span);
      }
    }
  }
}
 
Example 9
Source File: LargeAnimatedTextView.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
public void setText(CharSequence text, BufferType type) {
	_newText = new SpannableString(text);
	TextChar[] letters = _newText.getSpans(0, _newText.length(), TextChar.class);

	for (TextChar letter : letters) {
		_newText.removeSpan(letter);
	}
	for (int i = 0; i < _newText.length(); i++) {
		_newText.setSpan(new TextChar(getTextSize()), i, i + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
	}

	super.setText(_newText, BufferType.SPANNABLE);
}
 
Example 10
Source File: TimeLineUtility.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
public static SpannableString convertNormalStringToSpannableString(String txt) {
    // hack to fix android imagespan bug,see
    // http://stackoverflow.com/questions/3253148/imagespan-is-cut-off-incorrectly-aligned
    // if string only contains emotion tags,add a empty char to the end
    String hackTxt;
    if (txt.startsWith("[") && txt.endsWith("]")) {
        hackTxt = txt + " ";
    } else {
        hackTxt = txt;
    }
    // SpannableString value = SpannableString.valueOf(hackTxt);

    String formatted = formatLink(hackTxt);

    Spanned spanned = Html.fromHtml(formatted);

    SpannableString value = new SpannableString(spanned);

    Linkify.addLinks(value, WeiboPatterns.MENTION_URL, WeiboPatterns.MENTION_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.WEB_URL, WeiboPatterns.WEB_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.TOPIC_URL, WeiboPatterns.TOPIC_SCHEME);

    URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class);
    MyURLSpan weiboSpan;
    for (URLSpan urlSpan : urlSpans) {
        weiboSpan = new MyURLSpan(urlSpan.getURL());
        int start = value.getSpanStart(urlSpan);
        int end = value.getSpanEnd(urlSpan);
        value.removeSpan(urlSpan);
        value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    TimeLineUtility.addEmotions(value);
    return value;
}
 
Example 11
Source File: TimeLineUtility.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
public static SpannableString convertNormalStringToSpannableString(String txt) {
    // hack to fix android imagespan bug,see
    // http://stackoverflow.com/questions/3253148/imagespan-is-cut-off-incorrectly-aligned
    // if string only contains emotion tags,add a empty char to the end
    String hackTxt;
    if (txt.startsWith("[") && txt.endsWith("]")) {
        hackTxt = txt + " ";
    } else {
        hackTxt = txt;
    }
    // SpannableString value = SpannableString.valueOf(hackTxt);

    String formatted = formatLink(hackTxt);

    Spanned spanned = Html.fromHtml(formatted);

    SpannableString value = new SpannableString(spanned);

    Linkify.addLinks(value, WeiboPatterns.MENTION_URL, WeiboPatterns.MENTION_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.WEB_URL, WeiboPatterns.WEB_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.TOPIC_URL, WeiboPatterns.TOPIC_SCHEME);

    URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class);
    MyURLSpan weiboSpan;
    for (URLSpan urlSpan : urlSpans) {
        weiboSpan = new MyURLSpan(urlSpan.getURL());
        int start = value.getSpanStart(urlSpan);
        int end = value.getSpanEnd(urlSpan);
        value.removeSpan(urlSpan);
        value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    TimeLineUtility.addEmotions(value);
    return value;
}
 
Example 12
Source File: GameActivity.java    From TextFiction with Apache License 2.0 5 votes vote down vote up
/**
 * Add underlines to a text blob. Any existing underlines are removed. before
 * new ones are added.
 * 
 * @param span
 *          the blob to modify
 * @param words
 *          the words to underline (all lowercase!)
 */
private static void highlight(SpannableString span, String... words) {
	UnderlineSpan old[] = span.getSpans(0, span.length(), UnderlineSpan.class);
	for (UnderlineSpan del : old) {
		span.removeSpan(del);
	}
	char spanChars[] = span.toString().toLowerCase().toCharArray();
	for (String word : words) {
		char[] wc = word.toCharArray();
		int last = spanChars.length - wc.length + 1;
		for (int i = 0; i < last; i++) {
			// First check if there is a word-sized gap at spanchars[i] as we don't
			// want to highlight words that are actually just substrings (e.g.
			// "east" in "lEASTwise").
			if ((i > 0 && Character.isLetterOrDigit(spanChars[i - 1]))
					|| (i + wc.length != spanChars.length && Character.isLetterOrDigit(spanChars[i
							+ wc.length]))) {
				continue;
			}
			int a = i;
			int b = 0;
			while (b < wc.length) {
				if (spanChars[a] != wc[b]) {
					b = 0;
					break;
				}
				a++;
				b++;
			}
			if (b == wc.length) {
				span.setSpan(new UnderlineSpan(), i, a, 0);
				i = a;
			}
		}
	}
}
 
Example 13
Source File: SpannableStringHelper.java    From revolution-irc with GNU General Public License v3.0 4 votes vote down vote up
public static CharSequence copyCharSequence(CharSequence msg) {
    SpannableString str = new SpannableString(msg);
    for (Object o : str.getSpans(0, str.length(), NoCopySpan.class))
        str.removeSpan(o);
    return str;
}
 
Example 14
Source File: TimeLineUtility.java    From SprintNBA with Apache License 2.0 4 votes vote down vote up
public static SpannableString convertNormalStringToSpannableString(String txt, TimeLineStatus status) {
    String hackTxt;
    if (txt.startsWith("[") && txt.endsWith("]")) {
        hackTxt = txt + " ";
    } else {
        hackTxt = txt;
    }

    SpannableString value = SpannableString.valueOf(hackTxt);
    switch (status) {
        case LINK: {
            Linkify.addLinks(value, LinkPatterns.WEB_URL, LinkPatterns.WEB_SCHEME);
        }
        break;
        case FEED: {
            Linkify.addLinks(value, LinkPatterns.WEB_URL, LinkPatterns.WEB_SCHEME);
            Linkify.addLinks(value, LinkPatterns.TOPIC_URL, LinkPatterns.TOPIC_SCHEME);
            Linkify.addLinks(value, LinkPatterns.MENTION_URL, LinkPatterns.MENTION_SCHEME);
        }
        break;
    }

    android.text.style.URLSpan[] urlSpans = value.getSpans(0, value.length(), android.text.style.URLSpan.class);
    URLSpan weiboSpan;

    for (android.text.style.URLSpan urlSpan : urlSpans) {
        if (urlSpan.getURL().startsWith(LinkPatterns.TOPIC_SCHEME)) {
            String topic = urlSpan.getURL().substring(LinkPatterns.TOPIC_SCHEME.length(), urlSpan.getURL().length());
            //不识别空格话题和大于30字话题
            String group = topic.substring(1, topic.length() - 1).trim();
            if (1 > group.length() || group.length() > 30) {
                value.removeSpan(urlSpan);
                continue;
            }
        }
        weiboSpan = new URLSpan(urlSpan.getURL(), mColor);
        int start = value.getSpanStart(urlSpan);
        int end = value.getSpanEnd(urlSpan);
        value.removeSpan(urlSpan);
        value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return value;
}