Skip to content

Instantly share code, notes, and snippets.

@nekocode
Last active May 9, 2017 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekocode/9f8cbeb950b796d2663749e5375999ea to your computer and use it in GitHub Desktop.
Save nekocode/9f8cbeb950b796d2663749e5375999ea to your computer and use it in GitHub Desktop.
/**
* 裁剪文本
*/
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