java - Android 富文本 Mark Down
问题描述
我正在寻找有关使用 Mark Down 创建富文本的库或优化示例。
例如,要转的东西:
1 - *粗体* ->粗体
2 - _斜体_ ->斜体
长示例:
3 - _Hello_ *World* ->你好 世界
等等。
我见过很多应用程序都在使用它,比如 Discord、Whatsapp、Skype。
我做过类似的事情,但我知道它没有优化并且可能导致运行时错误。
解决方案
您不需要任何额外的库...
只是看看android.text.SpannableStringBuilder
...
这将允许您获得文本功能,例如:
- 让它变大
- 大胆的
- 强调
- 斜体
- 删除线
- 有色
- 突出显示
- 显示为上标
- 显示为下标
- 显示为链接
- 使其可点击。
这里有一个关于如何在 TextView 中的单词上应用粗体样式的示例:
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 - 无法使用 Intent 启动服务,无法添加窗口 - 令牌 null 无效,您的活动是否正在运行?
- php - 如何在没有 eval() 的情况下在 php 中创建动态链接
- excel - 保护关于单元格格式/边框的数据表
- ios - UINavigationController 无法同时推送两个 vc
- java - 如何使用 Spring 通过响应式 websocket 推送数据以响应请求?
- android - ionic cordova 运行 android 失败。不推荐使用未处理的承诺拒绝
- dart - 如何在 Flutter 上创建多个 Shared-Preferences?
- angular - 如何按需添加存在于不同角度应用程序中的角度组件(不使用库)?
- c# - 压缩图像后它最终旋转
- linux - linux内核中控制器中断号和中断号的关系