Use the first grapheme for the TextDrawable icon

Fixes #862
This commit is contained in:
Alexander Bakker 2022-01-03 19:52:36 +01:00
parent 86580e38af
commit f9b4e8fa3e

View file

@ -5,6 +5,7 @@ import android.view.View;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.text.BreakIterator;
import java.util.Arrays;
public class TextDrawableHelper {
@ -48,6 +49,18 @@ public class TextDrawableHelper {
.width(view.getLayoutParams().width)
.height(view.getLayoutParams().height)
.endConfig()
.buildRound(text.substring(0, 1).toUpperCase(), color);
.buildRound(getFirstGrapheme(text).toUpperCase(), color);
}
private static String getFirstGrapheme(String text) {
BreakIterator iter = BreakIterator.getCharacterInstance();
iter.setText(text);
int start = iter.first(), end = iter.next();
if (end == BreakIterator.DONE) {
return "";
}
return text.substring(start, end);
}
}