Skip to content

Instantly share code, notes, and snippets.

@nekocode nekocode/CutText.java
Last active May 9, 2017

Embed
What would you like to do?
/**
* 裁剪文本
*/
private static String cutText(float textSize, String title, float maxWidth) {
final TextPaint textPaint = new TextPaint();
textPaint.setTextSize(textSize);
if (textPaint.measureText(title) < maxWidth)
return title;
int i;
String str = null;
for (i = title.length(); i > 0; i--) {
str = title.substring(0, i) + "...";
float width = textPaint.measureText(str);
if (width < maxWidth) {
break;
}
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.