使用动态字符串将文本设为粗体、斜体和下划线
Making text Bold, Italic and Underscore with Dynamic string
我目前正在尝试弄清楚如何使文本 粗体 、 斜体 或带有来自 API,必须加粗的文本显示为 * 粗体 *,斜体显示为 _ italic_,下划线显示为 #underline#(与 Whosebug 的功能相同)。
文本转换成功后,我想把特殊字符也去掉。
Text from API -
* I am Bold* and love to see _myself and _ others too.
Expected answer - I am Bold and love to see myself and others too.
我尝试了一些代码,如果我尝试在粗体后创建斜体,如果我尝试删除特殊字符,这些代码也不起作用。
TextView t = findViewById(R.id.viewOne);
String text = "*I am Bold* and _I am Italic_ here *Bold too*";
SpannableStringBuilder b = new SpannableStringBuilder(text);
Matcher matcher = Pattern.compile(Pattern.quote("*") + "(.*?)" + Pattern.quote("*")).matcher(text);
while (matcher.find()){
String name = matcher.group(1);
int index = text.indexOf(name)-1;
b.setSpan(new StyleSpan(Typeface.BOLD), index, index + name.length()+1, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
}
t.setText(b);
我不想使用 HTML 标签
Edited answer to address the edited question
试试下面,你应该通过 typeface
而不是 StyleSpan
。
public class SpanTest extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView test = findViewById(R.id.test);
// String text = "*I am Bold* and _I am Italic_ here *Bold too*";
String text = "* I am Bold* and love to see _myself and _ others too";
CharSequence charSequence = updateSpan(text, "*", Typeface.BOLD);
charSequence = updateSpan(charSequence, "_", Typeface.ITALIC);
test.setText(charSequence);
}
private CharSequence updateSpan(CharSequence text, String delim, int typePace) {
Pattern pattern = Pattern.compile(Pattern.quote(delim) + "(.*?)" + Pattern.quote(delim));
SpannableStringBuilder builder = new SpannableStringBuilder(text);
if (pattern != null) {
Matcher matcher = pattern.matcher(text);
int matchesSoFar = 0;
while (matcher.find()) {
int start = matcher.start() - (matchesSoFar * 2);
int end = matcher.end() - (matchesSoFar * 2);
StyleSpan span = new StyleSpan(typePace);
builder.setSpan(span, start + 1, end - 1, 0);
builder.delete(start, start + 1);
builder.delete(end - 2, end - 1);
matchesSoFar++;
}
}
return builder;
}
}
这是输出。
我目前正在尝试弄清楚如何使文本 粗体 、 斜体 或带有来自 API,必须加粗的文本显示为 * 粗体 *,斜体显示为 _ italic_,下划线显示为 #underline#(与 Whosebug 的功能相同)。 文本转换成功后,我想把特殊字符也去掉。
Text from API - * I am Bold* and love to see _myself and _ others too.
Expected answer - I am Bold and love to see myself and others too.
我尝试了一些代码,如果我尝试在粗体后创建斜体,如果我尝试删除特殊字符,这些代码也不起作用。
TextView t = findViewById(R.id.viewOne);
String text = "*I am Bold* and _I am Italic_ here *Bold too*";
SpannableStringBuilder b = new SpannableStringBuilder(text);
Matcher matcher = Pattern.compile(Pattern.quote("*") + "(.*?)" + Pattern.quote("*")).matcher(text);
while (matcher.find()){
String name = matcher.group(1);
int index = text.indexOf(name)-1;
b.setSpan(new StyleSpan(Typeface.BOLD), index, index + name.length()+1, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
}
t.setText(b);
我不想使用 HTML 标签
Edited answer to address the edited question
试试下面,你应该通过 typeface
而不是 StyleSpan
。
public class SpanTest extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView test = findViewById(R.id.test);
// String text = "*I am Bold* and _I am Italic_ here *Bold too*";
String text = "* I am Bold* and love to see _myself and _ others too";
CharSequence charSequence = updateSpan(text, "*", Typeface.BOLD);
charSequence = updateSpan(charSequence, "_", Typeface.ITALIC);
test.setText(charSequence);
}
private CharSequence updateSpan(CharSequence text, String delim, int typePace) {
Pattern pattern = Pattern.compile(Pattern.quote(delim) + "(.*?)" + Pattern.quote(delim));
SpannableStringBuilder builder = new SpannableStringBuilder(text);
if (pattern != null) {
Matcher matcher = pattern.matcher(text);
int matchesSoFar = 0;
while (matcher.find()) {
int start = matcher.start() - (matchesSoFar * 2);
int end = matcher.end() - (matchesSoFar * 2);
StyleSpan span = new StyleSpan(typePace);
builder.setSpan(span, start + 1, end - 1, 0);
builder.delete(start, start + 1);
builder.delete(end - 2, end - 1);
matchesSoFar++;
}
}
return builder;
}
}
这是输出。