如何在保持格式的同时拆分 SpannableStringBuilder?
How to split up a SpannableStringBuilder while keeping its formating?
我正在从事一个 android 项目,该项目涉及将一些 HTML(由 Jsoup 解析)解析为 SpannableStringBuilder class。
但是,我需要这个 SpannableStringBuilder class 在完成解析后按每个换行符分成一个列表,同时保持其格式。
这样
的跨文本
{"I am a spanned text,\n hear me roar"}
会变成
{
"I am a spanned text,"
"hear me roar"
}
我对在 Android 上进行开发还很陌生,在文档中找不到任何关于 spitting span 的内容,甚至无法获得 spanned class 上所有格式的列表来构建我自己的。因此,非常感谢任何帮助。
在研究了 pskink 建议的方法后,我设法自己解决了这个问题。
我的解决方案是
@Override
public List<Spanned> parse() {
List<Spanned> spans = new ArrayList<Spanned>();
Spannable unsegmented = (Spannable) Html.fromHtml(base.html(), null, new ReaderTagHandler());
//Set ColorSpan because it defaults to white text color
unsegmented.setSpan(new ForegroundColorSpan(Color.BLACK), 0, unsegmented.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//get locations of '/n'
Stack<Integer> loc = getNewLineLocations(unsegmented);
loc.push(unsegmented.length());
//divides up a span by each new line character position in loc
while (!loc.isEmpty()) {
Integer end = loc.pop();
Integer start = loc.isEmpty() ? 0 : loc.peek();
spans.add(0,(Spanned) unsegmented.subSequence(start, end));
}
return spans;
}
private Stack<Integer> getNewLineLocations(Spanned unsegmented) {
Stack<Integer> loc = new Stack<>();
String string = unsegmented.toString();
int next = string.indexOf('\n');
while (next > 0) {
//avoid chains of newline characters
if (string.charAt(next - 1) != '\n') {
loc.push(next);
next = string.indexOf('\n', loc.peek() + 1);
} else {
next = string.indexOf('\n', next + 1);
}
if (next >= string.length()) next = -1;
}
return loc;
}
我正在从事一个 android 项目,该项目涉及将一些 HTML(由 Jsoup 解析)解析为 SpannableStringBuilder class。
但是,我需要这个 SpannableStringBuilder class 在完成解析后按每个换行符分成一个列表,同时保持其格式。
这样
的跨文本{"I am a spanned text,\n hear me roar"}
会变成
{ "I am a spanned text," "hear me roar" }
我对在 Android 上进行开发还很陌生,在文档中找不到任何关于 spitting span 的内容,甚至无法获得 spanned class 上所有格式的列表来构建我自己的。因此,非常感谢任何帮助。
在研究了 pskink 建议的方法后,我设法自己解决了这个问题。
我的解决方案是
@Override
public List<Spanned> parse() {
List<Spanned> spans = new ArrayList<Spanned>();
Spannable unsegmented = (Spannable) Html.fromHtml(base.html(), null, new ReaderTagHandler());
//Set ColorSpan because it defaults to white text color
unsegmented.setSpan(new ForegroundColorSpan(Color.BLACK), 0, unsegmented.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//get locations of '/n'
Stack<Integer> loc = getNewLineLocations(unsegmented);
loc.push(unsegmented.length());
//divides up a span by each new line character position in loc
while (!loc.isEmpty()) {
Integer end = loc.pop();
Integer start = loc.isEmpty() ? 0 : loc.peek();
spans.add(0,(Spanned) unsegmented.subSequence(start, end));
}
return spans;
}
private Stack<Integer> getNewLineLocations(Spanned unsegmented) {
Stack<Integer> loc = new Stack<>();
String string = unsegmented.toString();
int next = string.indexOf('\n');
while (next > 0) {
//avoid chains of newline characters
if (string.charAt(next - 1) != '\n') {
loc.push(next);
next = string.indexOf('\n', loc.peek() + 1);
} else {
next = string.indexOf('\n', next + 1);
}
if (next >= string.length()) next = -1;
}
return loc;
}