android.text.SpannableString#removeSpan ( )源码实例Demo

下面列出了android.text.SpannableString#removeSpan ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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;
}
 
@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;
}
 
源代码3 项目: Zom-Android-XMPP   文件: LinkifyHelper.java
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);
}
 
源代码4 项目: talkback   文件: TextEventInterpreter.java
@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;
}
 
源代码5 项目: tilt-game-android   文件: SmallAnimatedTextView.java
@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);
}
 
源代码6 项目: openshop.io-android   文件: Utils.java
/**
 * 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;
}
 
源代码7 项目: QuickDevFramework   文件: SpanFormatter.java
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;
	}
}
 
源代码8 项目: talkback   文件: SpannableUtils.java
/**
 * 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);
      }
    }
  }
}
 
源代码9 项目: tilt-game-android   文件: LargeAnimatedTextView.java
@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);
}
 
源代码10 项目: iBeebo   文件: TimeLineUtility.java
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;
}
 
源代码11 项目: iBeebo   文件: TimeLineUtility.java
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;
}
 
源代码12 项目: TextFiction   文件: GameActivity.java
/**
 * 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;
			}
		}
	}
}
 
源代码13 项目: revolution-irc   文件: SpannableStringHelper.java
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;
}
 
源代码14 项目: SprintNBA   文件: TimeLineUtility.java
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;
}