Skip to content

Commit f79f270

Browse files
committed
Remove some end punctuation when adding the ellipsis
1 parent 0dbe990 commit f79f270

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/com/triposo/barone/EllipsizingTextView.java

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.regex.Pattern;
2223

2324
import android.content.Context;
2425
import android.content.res.TypedArray;
@@ -32,6 +33,7 @@
3233

3334
public class EllipsizingTextView extends TextView {
3435
private static final String ELLIPSIS = "…";
36+
private static final Pattern DEFAULT_END_PUNCTUATION = Pattern.compile("[\\.,…;\\:\\s]*$", Pattern.DOTALL);
3537

3638
public interface EllipsizeListener {
3739
void ellipsizeStateChanged(boolean ellipsized);
@@ -45,6 +47,10 @@ public interface EllipsizeListener {
4547
private int maxLines;
4648
private float lineSpacingMultiplier = 1.0f;
4749
private float lineAdditionalVerticalPadding = 0.0f;
50+
/**
51+
* The end punctuation which will be removed when appending #ELLIPSIS.
52+
*/
53+
private Pattern endPunctuationPattern;
4854

4955
public EllipsizingTextView(Context context) {
5056
this(context, null);
@@ -59,6 +65,11 @@ public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
5965
super.setEllipsize(null);
6066
TypedArray a = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.maxLines });
6167
setMaxLines(a.getInt(0, Integer.MAX_VALUE));
68+
setEndPunctuationPattern(DEFAULT_END_PUNCTUATION);
69+
}
70+
71+
public void setEndPunctuationPattern(Pattern pattern) {
72+
this.endPunctuationPattern = pattern;
6273
}
6374

6475
public void addEllipsizeListener(EllipsizeListener listener) {
@@ -146,6 +157,8 @@ private void resetText() {
146157
}
147158
workingText = workingText.substring(0, lastSpace);
148159
}
160+
// We should do this in the loop above, but it's cheaper this way.
161+
workingText = endPunctuationPattern.matcher(workingText).replaceFirst("");
149162
workingText = workingText + ELLIPSIS;
150163
ellipsized = true;
151164
}

0 commit comments

Comments
 (0)