简体   繁体   English

Android富文本Mark Down

[英]Android rich text Mark Down

I'm looking for a library or optimized examples on using Mark Down to create Rich Text. 我正在寻找使用Mark Down创建Rich Text的库或优化示例。

For example, something to turn: 例如,要转向:

1 - *Bold* -> Bold 1 - * Bold * - > Bold

2 - _Italic_ -> Italic 2 - _Italic_ - > 斜体

Long example: 很长的例子:

3 - _Hello_ *World* -> Hello World 3 - _Hello_ *世界* - > Hello World

And so on. 等等。

I've seen many apps use it, like Discord, Whatsapp, Skype. 我见过许多应用程序使用它,如Discord,Whatsapp,Skype。

I've done something similar, but I know it's not optimized and can lead to runtime errors. 我做了类似的事情,但我知道它没有优化,可能导致运行时错误。

You don't need any extra library... 你不需要任何额外的库......

Just have a look into android.text.SpannableStringBuilder ... 看看android.text.SpannableStringBuilder ......

This will allow you to get text features like: 这将允许您获得如下文本功能:

  • Make it larger 把它做大
  • Bold 胆大
  • Underline 强调
  • Italicize 斜体
  • Strike-through 划掉
  • Colored 有色
  • Highlighted 突出
  • Show as superscript 显示为上标
  • Show as subscript 显示为下标
  • Show as a link 显示为链接
  • Make it clickable. 让它可点击。

Here you have an example on how to apply a Bold style on a word in a TextView : 这里有一个关于如何在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);        
SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start_int, end_int, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv=new TextView(context);
tv.setText(str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM