椭圆化 TextView 中 SpannableString 的单行 [英] Ellipsize individual lines of a SpannableString in a TextView
问题描述
我有一个 TextView
存储一个两行长的地址.这条街在第一行并且是粗体的.城市与状态在第二行并且不是粗体.如果该行上的单个文本超出,我希望每行的末尾都被省略.我正在使用 SpannableString
来存储地址,因为我希望街道地址为粗体,而城市 &状态不要粗体.
I have a TextView
storing an address that is two lines long. The street goes on the first line and is bold. The city & state go on the second line and are not bold. I want the end of each line ellipsized if the individual text on that line runs over. I'm using a SpannableString
to store the address because I want the street address to be bold and the city & state to be not bold.
有没有一种方法可以在不为每行使用两个 TextViews
的情况下做到这一点?
Is there a way to do this that isn't a total hack without using two TextViews
for each line?
示例输出:
例如:
123812 华盛顿...
纽约斯克内克塔迪...
Ex:
123812 Washington A...
Schenectady New Yor...
例如:
2792 丹茨勒大道...
查尔斯顿 SC,29406
Ex:
2792 Dantzler Boulev...
Charleston SC, 29406
例如:
3大街
乔治亚州亚特兰大
Ex:
3 Main Street
Atlanta GA
推荐答案
我有同样的问题,并通过创建一个 EllipsizeLineSpan 类解决了它.你可以用它包裹你想要省略的每一行.
I had the same problem and solved it by creating an EllipsizeLineSpan class. You can wrap each line that you want to ellipsize with it.
用它标记可跨字符串的示例:
Example for marking up a spannable string with it:
SpannableStringBuilder textspan = new SpannableStringBuilder("#1.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"+
"Protect from ellipsizing #2.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"+
"#3.Lorem ipsum dolor sit amet, consectetur adipisicing elit
"+
"#4.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
");
// find ellipsizable text (from '#' to newline)
Pattern pattern = Pattern.compile("#.*\n", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textspan);
while(matcher.find()) {
textspan.setSpan(new EllipsizeLineSpan(), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
椭圆大小线跨度:
public class EllipsizeLineSpan extends ReplacementSpan implements LineBackgroundSpan {
int layoutLeft = 0;
int layoutRight = 0;
public EllipsizeLineSpan () {
}
@Override
public void drawBackground (Canvas c, Paint p,
int left, int right,
int top, int baseline, int bottom,
CharSequence text, int start, int end,
int lnum) {
Rect clipRect = new Rect();
c.getClipBounds(clipRect);
layoutLeft = clipRect.left;
layoutRight = clipRect.right;
}
@Override
public int getSize (Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
return layoutRight - layoutLeft;
}
@Override
public void draw (Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
float textWidth = paint.measureText(text, start, end);
if (x + (int) Math.ceil(textWidth) < layoutRight) { //text fits
canvas.drawText(text, start, end, x, y, paint);
} else {
float ellipsiswid = paint.measureText("u2026");
// move 'end' to the ellipsis point
end = start + paint.breakText(text, start, end, true, layoutRight - x - ellipsiswid, null);
canvas.drawText(text, start, end, x, y, paint);
canvas.drawText("u2026", x + paint.measureText(text, start, end), y, paint);
}
}
}
这篇关于椭圆化 TextView 中 SpannableString 的单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!