truncateString static method
Truncate the given String to maxLength using String.length
(not user-perceived characters).
See also:
Implementation
static String truncateString(String value, int maxLength) {
//
// If the text fits the available space, use it all.
if (value.length <= maxLength)
return value;
//
// If the text doesn't fit the available space, we must cut it,
// but do not cut a grapheme in half.
else {
List<String> chars = [];
int length = 0;
for (String char in value.characters) {
length += char.length;
if (length <= maxLength) {
chars.add(char);
}
}
return chars.join();
}
}