我正在寻找有关使用的库或优化示例 Mark Down 以创建富文本。
例如,要转的东西:
1 - *粗体* -> 粗体
2 - _Italic_ -> Italic
长例子:
3 - _Hello_ *World* -> Hello World
等等。
我见过很多应用程序都在使用它,例如 Discord、Whatsapp、Skype。
我做过类似的事情,但我知道它没有经过优化,可能会导致运行时错误。
最佳答案
你不需要任何额外的库...
看看 android.text.SpannableStringBuilder
...
这将使您获得文本功能,例如:
- 让它变大
- 粗体
- 下划线
- 斜体
- 删除线
- 有色人种
- 突出显示
- 显示为上标
- 显示为下标
- 显示为链接
- 使其可点击。
这里有一个关于如何在 TextView 中的单词上应用 Bold 样式的示例:
String text = "This is an example with a Bold word...";
// Initialize a new SpannableStringBuilder instance
SpannableStringBuilder strb = new SpannableStringBuilder(text);
// Initialize a new StyleSpan to display bold text
StyleSpan bSpan = new StyleSpan(Typeface.BOLD);
// The index where to start applying the Bold Span
int idx = text.indexOf("Bold");
strb.setSpan(
bSpan, // Span to add
idx, // Start of the span
idx + 4, // End of the span
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
// Display the spannable text to yourTextView
yourTextView.setText(ssBuilder);
关于java - Android 富文本 Markdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53294724/