下面列出了android.text.style.ScaleXSpan#android.text.style.StrikethroughSpan 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void endCssStyle(Editable text) {
Strikethrough s = getLast(text, Strikethrough.class);
if (s != null) {
setSpanFromMark(text, s, new StrikethroughSpan());
}
Background b = getLast(text, Background.class);
if (b != null) {
setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
}
Foreground f = getLast(text, Foreground.class);
if (f != null) {
setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
}
}
@Test
public void testTextDecorationLineLineThroughApplied() {
UIManagerModule uiManager = getUIManagerModule();
ReactRootView rootView = createText(
uiManager,
JavaOnlyMap.of(ViewProps.TEXT_DECORATION_LINE, "line-through"),
JavaOnlyMap.of(ReactRawTextShadowNode.PROP_TEXT, "test text"));
TextView textView = (TextView) rootView.getChildAt(0);
Spanned text = (Spanned) textView.getText();
UnderlineSpan[] underlineSpans =
text.getSpans(0, text.length(), UnderlineSpan.class);
StrikethroughSpan strikeThroughSpan =
getSingleSpan(textView, StrikethroughSpan.class);
assertThat(underlineSpans).hasSize(0);
assertThat(strikeThroughSpan instanceof StrikethroughSpan).isTrue();
}
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (opening) {
if (tag.equalsIgnoreCase(BULLET_LI)) {
if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
output.append("\n");
}
start(output, new Li());
} else if (tag.equalsIgnoreCase(STRIKETHROUGH_S) || tag.equalsIgnoreCase(STRIKETHROUGH_STRIKE) || tag.equalsIgnoreCase(STRIKETHROUGH_DEL)) {
start(output, new Strike());
}
} else {
if (tag.equalsIgnoreCase(BULLET_LI)) {
if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
output.append("\n");
}
end(output, Li.class, new BulletSpan());
} else if (tag.equalsIgnoreCase(STRIKETHROUGH_S) || tag.equalsIgnoreCase(STRIKETHROUGH_STRIKE) || tag.equalsIgnoreCase(STRIKETHROUGH_DEL)) {
end(output, Strike.class, new StrikethroughSpan());
}
}
}
@Override
public void handleMarkdownSpan(
@NonNull PersistedSpans persistedSpans,
@NonNull Editable editable,
@NonNull String input,
@NonNull StrikethroughSpan span,
int spanStart,
int spanTextLength) {
final MarkwonEditorUtils.Match match =
MarkwonEditorUtils.findDelimited(input, spanStart, "~~");
if (match != null) {
editable.setSpan(
persistedSpans.get(StrikethroughSpan.class),
match.start(),
match.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}
private ImageView getHtmloptionToolButton(Object span) {
if (span instanceof StyleSpan) {
switch (((StyleSpan) span).getStyle()) {
case Typeface.BOLD:
return (ImageView) findViewById(R.id.makeBold);
case Typeface.ITALIC:
return (ImageView) findViewById(R.id.makeItalic);
default:
return null;
}
} else if (span instanceof UnderlineSpan) {
return (ImageView) findViewById(R.id.makeUnderline);
}
else if(span instanceof StrikethroughSpan)
return (ImageView) findViewById(R.id.makeStrikethrough);
return null;
}
private void endCssStyle(String tag, Editable text) {
Strikethrough s = getLast(text, Strikethrough.class);
if (s != null) {
setSpanFromMark(tag, text, s, new StrikethroughSpan());
}
Background b = getLast(text, Background.class);
if (b != null) {
setSpanFromMark(tag, text, b, new BackgroundColorSpan(b.mBackgroundColor));
}
Foreground f = getLast(text, Foreground.class);
if (f != null) {
setSpanFromMark(tag, text, f, new ForegroundColorSpan(f.mForegroundColor));
}
AbsoluteSize a = getLast(text, AbsoluteSize.class);
if (a != null) {
setSpanFromMark(tag, text, a, new AbsoluteSizeSpan(a.getTextSize()));
}
RelativeSize r = getLast(text, RelativeSize.class);
if (r != null) {
setSpanFromMark(tag, text, r, new RelativeSizeSpan(r.getTextProportion()));
}
}
private static void endCssStyle(Editable text) {
Font font = getLast(text, Font.class);
if (font != null) {
if (font.mFace.equalsIgnoreCase("fontello")) {
setSpanFromMark(text, font, new AssetsTypefaceSpan(App.getContext(), "fontello/fontello.ttf"));
}
}
Strikethrough s = getLast(text, Strikethrough.class);
if (s != null) {
setSpanFromMark(text, s, new StrikethroughSpan());
}
Background b = getLast(text, Background.class);
if (b != null) {
setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
}
Foreground f = getLast(text, Foreground.class);
if (f != null) {
setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
}
}
private void handleEndTag(CharacterStyle style) {
if (style instanceof URLSpan) {
mOut.append("</a>");
} else if (style instanceof TypefaceSpan) {
mOut.append("</font>");
} else if (style instanceof ForegroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof BackgroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof AbsoluteSizeSpan) {
mOut.append("</font>");
} else if (style instanceof StrikethroughSpan) {
mOut.append("</strike>");
} else if (style instanceof SubscriptSpan) {
mOut.append("</sub>");
} else if (style instanceof SuperscriptSpan) {
mOut.append("</sup>");
} else if (style instanceof UnderlineSpan) {
mOut.append("</u>");
} else if (style instanceof BoldSpan) {
mOut.append("</b>");
} else if (style instanceof ItalicSpan) {
mOut.append("</i>");
}
}
private void handleEndTag(CharacterStyle style) {
if (style instanceof URLSpan) {
mOut.append("</a>");
} else if (style instanceof TypefaceSpan) {
mOut.append("</font>");
} else if (style instanceof ForegroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof BackgroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof AbsoluteSizeSpan) {
mOut.append("</font>");
} else if (style instanceof StrikethroughSpan) {
mOut.append("</strike>");
} else if (style instanceof SubscriptSpan) {
mOut.append("</sub>");
} else if (style instanceof SuperscriptSpan) {
mOut.append("</sup>");
} else if (style instanceof UnderlineSpan) {
mOut.append("</u>");
} else if (style instanceof BoldSpan) {
mOut.append("</b>");
} else if (style instanceof ItalicSpan) {
mOut.append("</i>");
}
}
/**
* 现金券显示价格样式
*/
public static SpannableString getCashPrice(Context context, double oldPrice, double newPrice) {
StringBuilder builder = new StringBuilder();
builder.append(handleDouble(newPrice)).append("元").append(" ").append(handleDouble(oldPrice)).append("元");
int start = 0;
int middle = builder.indexOf(" ") + 1;
int end = builder.length();
SpannableString string = new SpannableString(builder);
/*改变文字的大小*/
string.setSpan(new AbsoluteSizeSpan(sp2px(context, 20)), start, middle, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new AbsoluteSizeSpan(sp2px(context, 14)), middle, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
/*给文字设置删除线*/
string.setSpan(new StrikethroughSpan(), middle, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
/*改变文字的颜色*/
int textOrange = context.getResources().getColor(android.R.color.holo_red_light);
int textGray = context.getResources().getColor(android.R.color.darker_gray);
string.setSpan(new ForegroundColorSpan(textOrange), start, middle, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new ForegroundColorSpan(textGray), middle, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return string;
}
@Override
public Spannable getDisplayedTime(Context context) {
if (displayedTime == null) {
SpannableStringBuilder builder = new SpannableStringBuilder(dead ?
context.getString(R.string.dead_prefix) + " " : "");
SpannableString timeSpannable = new SpannableString(
AppUtils.getAbbreviatedTimeSpan(time * 1000));
if (deleted) {
timeSpannable.setSpan(new StrikethroughSpan(), 0, timeSpannable.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
builder.append(timeSpannable);
displayedTime = builder;
}
return displayedTime;
}
private void handleEndTag(CharacterStyle style) {
if (style instanceof URLSpan) {
mOut.append("</a>");
} else if (style instanceof TypefaceSpan) {
mOut.append("</font>");
} else if (style instanceof ForegroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof BackgroundColorSpan) {
mOut.append("</font>");
} else if (style instanceof AbsoluteSizeSpan) {
mOut.append("</font>");
} else if (style instanceof StrikethroughSpan) {
mOut.append("</strike>");
} else if (style instanceof SubscriptSpan) {
mOut.append("</sub>");
} else if (style instanceof SuperscriptSpan) {
mOut.append("</sup>");
} else if (style instanceof UnderlineSpan) {
mOut.append("</u>");
} else if (style instanceof BoldSpan) {
mOut.append("</b>");
} else if (style instanceof ItalicSpan) {
mOut.append("</i>");
}
}
private Object[] getSpanType(Object span, Object[] result) {
result[0] = 0;
result[1] = null;
if (span instanceof LinkSpan) {
result[0] = ChanMarkup.TAG_SPECIAL_LINK;
result[1] = ((LinkSpan) span).getUriString();
} else if (span instanceof SpoilerSpan) {
result[0] = ChanMarkup.TAG_SPOILER;
} else if (span instanceof QuoteSpan) {
result[0] = ChanMarkup.TAG_QUOTE;
} else if (span instanceof ScriptSpan) {
result[0] = ((ScriptSpan) span).isSuperscript() ? ChanMarkup.TAG_SUPERSCRIPT : ChanMarkup.TAG_SUBSCRIPT;
} else if (span instanceof StyleSpan) {
int style = ((StyleSpan) span).getStyle();
if (style == Typeface.BOLD) {
result[0] = ChanMarkup.TAG_BOLD;
} else {
if (style == Typeface.ITALIC) {
result[0] = ChanMarkup.TAG_ITALIC;
}
}
} else if (span instanceof UnderlineSpan) {
result[0] = ChanMarkup.TAG_UNDERLINE;
} else if (span instanceof OverlineSpan) {
result[0] = ChanMarkup.TAG_OVERLINE;
} else if (span instanceof StrikethroughSpan) {
result[0] = ChanMarkup.TAG_STRIKE;
} else if (span instanceof GainedColorSpan) {
result[0] = ChanMarkup.TAG_SPECIAL_COLOR;
result[1] = ((GainedColorSpan) span).getForegroundColor();
} else if (span instanceof MonospaceSpan) {
result[0] = ((MonospaceSpan) span).isAsciiArt() ? ChanMarkup.TAG_ASCII_ART : ChanMarkup.TAG_CODE;
} else if (span instanceof HeadingSpan) {
result[0] = ChanMarkup.TAG_HEADING;
}
return result;
}
private boolean markInvalidWord(final Editable e) {
for (final StrikethroughSpan s : e.getSpans(0, e.length(), StrikethroughSpan.class))
e.removeSpan(s);
final String word = e.toString();
if (isHexSeed(word))
return true;
final int end = word.length();
if (!MnemonicHelper.isPrefix(word))
e.setSpan(new StrikethroughSpan(), 0, end, 0);
return !MnemonicHelper.mWords.contains(word);
}
public static SpannableString getPrict(String price1,String price2){
String s = "¥" + price1 + " 原价" + price2;
int pos = s.indexOf(" ");
int end = s.length();
SpannableString spanString = new SpannableString(s);
ForegroundColorSpan span = new ForegroundColorSpan(0xff999999);
StrikethroughSpan span2 = new StrikethroughSpan();
spanString.setSpan(span, pos+1, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanString.setSpan(span2, pos+1, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanString;
}
private static ParcelableSpan clone(ParcelableSpan span) {
if (span instanceof ForegroundColorSpan) {
return new ForegroundColorSpan(((ForegroundColorSpan) span).getForegroundColor());
} else if (span instanceof TypefaceSpan) {
return new TypefaceSpan(((TypefaceSpan) span).getFamily());
} else if (span instanceof StyleSpan) {
return new StyleSpan(((StyleSpan) span).getStyle());
} else if (span instanceof StrikethroughSpan) {
return new StrikethroughSpan();
} else {
throw new AssertionError("Unknown Span");
}
}
/**
* 为多个位置的文字添加删除线
* @param text
* @param startPos
* @param len
* @return
*/
public static SpannableString getStrikeThroughSpannable(String text, int[] startPos, int[] len) {
SpannableString span = new SpannableString(text);
if (startPos != null && len != null && startPos.length > 0 && len.length > 0 && startPos.length == len.length) {
for (int i = 0; i < startPos.length; i++) {
span.setSpan(new StrikethroughSpan(), startPos[i], startPos[i]+len[i], Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
}
return span;
}
protected void strikethroughValid(int start, int end) {
if (start >= end) {
return;
}
getEditableText().setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
html = ((TextView) findViewById(R.id.html));
flavoredHtml = new FlavoredHtml.Builder(this)
.newLine("p", "h1", "h2", "h3", "h4", "h5", "h6", "li")
.textAppearance(R.style.headline, "h1")
.textAppearance(R.style.title, "h2")
.textAppearance(R.style.subhead, "h3")
.textAppearance(R.style.body, "p", "li")
.style(Typeface.BOLD, "b", "strong")
.style(Typeface.ITALIC, "i", "em")
.bullet(15, "li")
.img(new ImgLoader.BitmapLoader(getResources()) {
@Override
protected Bitmap loadBitmap(String src) {
try {
return Picasso.with(MainActivity.this)
.load(src)
.get();
} catch (IOException e) {
throw new RuntimeException("Whoops!");
}
}
})
.blockStyle(new BlockStyleListener("s") {
@Override
protected Object getStyleSpan() {
return new StrikethroughSpan();
}
})
.build();
}
private String spanToMarkDown(List<Object> mSpans, String source, int start, int end) {
if (mSpans == null || mSpans.size() == 0) {
return source.substring(start, end);
}
String str = source.substring(start, end);
for (Object obj : mSpans) {
if (obj instanceof StyleSpan) {
StyleSpan objSpan = (StyleSpan) obj;
switch (objSpan.getStyle()) {
case Typeface.BOLD:
str = formater.formatBold(str);
break;
case Typeface.ITALIC:
str = formater.formatItalics(str);
break;
}
} else if (obj instanceof UnderlineSpan) {
//markdown是不支持下划线的!!!,后期考虑注释掉
str = formater.formatUnderLine(str);
} else if (obj instanceof StrikethroughSpan) {
str = formater.formatCenterLine(str);
} else if (obj instanceof URLSpanNoUnderline) {
URLSpanNoUnderline url = (URLSpanNoUnderline) obj;
str = formater.formatLink(str, url.getURL());
}
}
return str;
}
@Test
public void strikethrough_should_add_only_one_span() {
spanBuilder.style(range, EZ.STRIKETHROUGH)
.apply();
verify((SpanEZ) spanBuilder, times(1))
.addSpan(isA(TargetRange.class), isA(StrikethroughSpan.class));
}
/**
* @see StrikethroughSpan#StrikethroughSpan()
*/
public static Span strikeThrough() {
return new Span(new SpanBuilder() {
@Override
public Object build() {
return new StrikethroughSpan();
}
});
}
private void processStrike(boolean opening, Editable output) {
int len = output.length();
if (opening) {
output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, StrikethroughSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
switch (style.getKeyword()) {
case "*":
return new StyleSpan(Typeface.BOLD);
case "_":
return new StyleSpan(Typeface.ITALIC);
case "~":
return new StrikethroughSpan();
case "`":
case "```":
return new TypefaceSpan("monospace");
default:
throw new AssertionError("Unknown Style");
}
}
private void processStrike(boolean opening, Editable output) {
int len = output.length();
if (opening) {
output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, StrikethroughSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
/**
* Create a task list which contains {@link SetSpanOperation}. The task list will be executed
* in other method.
* @param end the end character of the text.
* @return a task list which contains {@link SetSpanOperation}.
*/
private List<SetSpanOperation> createSetSpanOperation(int end, int spanFlag) {
List<SetSpanOperation> ops = new LinkedList<>();
int start = 0;
if (end >= start) {
if (mTextDecoration == WXTextDecoration.UNDERLINE) {
ops.add(new SetSpanOperation(start, end,
new UnderlineSpan(), spanFlag));
}
if (mTextDecoration == WXTextDecoration.LINETHROUGH) {
ops.add(new SetSpanOperation(start, end,
new StrikethroughSpan(), spanFlag));
}
if (mIsColorSet) {
ops.add(new SetSpanOperation(start, end,
new ForegroundColorSpan(mColor), spanFlag));
}
if (mFontSize != UNSET) {
ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(mFontSize), spanFlag));
}
if (mFontStyle != UNSET
|| mFontWeight != UNSET
|| mFontFamily != null) {
ops.add(new SetSpanOperation(start, end,
new WXCustomStyleSpan(mFontStyle, mFontWeight, mFontFamily),
spanFlag));
}
ops.add(new SetSpanOperation(start, end, new AlignmentSpan.Standard(mAlignment), spanFlag));
if (mLineHeight != UNSET) {
ops.add(new SetSpanOperation(start, end, new WXLineHeightSpan(mLineHeight), spanFlag));
}
}
return ops;
}
public TextDecorator strikethrough(final String... texts) {
int index;
for (String text : texts) {
if (content.contains(text)) {
index = content.indexOf(text);
decoratedContent.setSpan(new StrikethroughSpan(), index, index + text.length(), flags);
}
}
return this;
}
public SpannableString spannableString(String str, boolean color) {
final SpannableString ret = new SpannableString((str != null) ? str : "");
if (isStale()) wholeSpan(ret, new StrikethroughSpan());
if (color) {
if (isLow()) {
// TODO should colors be configurable?
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#C30909")));
} else if (isHigh()) {
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#FFBB33")));
} // else default to whatever default is?
}
return ret;
}
public SpannableString spannableString(String str, boolean color) {
final SpannableString ret = new SpannableString((str != null) ? str : "");
if (isStale()) wholeSpan(ret, new StrikethroughSpan());
if (color) {
if (isLow()) {
// TODO should colors be configurable?
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#C30909")));
} else if (isHigh()) {
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#FFBB33")));
} // else default to whatever default is?
}
return ret;
}
public SpannableString spannableString(String str, boolean color) {
final SpannableString ret = new SpannableString((str != null) ? str : "");
if (isStale()) wholeSpan(ret, new StrikethroughSpan());
if (color) {
if (isLow()) {
// TODO should colors be configurable?
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#C30909")));
} else if (isHigh()) {
wholeSpan(ret, new ForegroundColorSpan(Color.parseColor("#FFBB33")));
} // else default to whatever default is?
}
return ret;
}