19
19
20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
+ import java .util .regex .Pattern ;
22
23
23
24
import android .content .Context ;
24
25
import android .content .res .TypedArray ;
32
33
33
34
public class EllipsizingTextView extends TextView {
34
35
private static final String ELLIPSIS = "…" ;
36
+ private static final Pattern DEFAULT_END_PUNCTUATION = Pattern .compile ("[\\ .,…;\\ :\\ s]*$" , Pattern .DOTALL );
35
37
36
38
public interface EllipsizeListener {
37
39
void ellipsizeStateChanged (boolean ellipsized );
@@ -45,6 +47,10 @@ public interface EllipsizeListener {
45
47
private int maxLines ;
46
48
private float lineSpacingMultiplier = 1.0f ;
47
49
private float lineAdditionalVerticalPadding = 0.0f ;
50
+ /**
51
+ * The end punctuation which will be removed when appending #ELLIPSIS.
52
+ */
53
+ private Pattern endPunctuationPattern ;
48
54
49
55
public EllipsizingTextView (Context context ) {
50
56
this (context , null );
@@ -59,6 +65,11 @@ public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
59
65
super .setEllipsize (null );
60
66
TypedArray a = context .obtainStyledAttributes (attrs , new int [] { android .R .attr .maxLines });
61
67
setMaxLines (a .getInt (0 , Integer .MAX_VALUE ));
68
+ setEndPunctuationPattern (DEFAULT_END_PUNCTUATION );
69
+ }
70
+
71
+ public void setEndPunctuationPattern (Pattern pattern ) {
72
+ this .endPunctuationPattern = pattern ;
62
73
}
63
74
64
75
public void addEllipsizeListener (EllipsizeListener listener ) {
@@ -146,6 +157,8 @@ private void resetText() {
146
157
}
147
158
workingText = workingText .substring (0 , lastSpace );
148
159
}
160
+ // We should do this in the loop above, but it's cheaper this way.
161
+ workingText = endPunctuationPattern .matcher (workingText ).replaceFirst ("" );
149
162
workingText = workingText + ELLIPSIS ;
150
163
ellipsized = true ;
151
164
}
0 commit comments