private String hackSpace(Context context, float textSize, String text) {
TextView textView = new TextView(context);
textView.setTextSize(textSize);
ViewGroup.LayoutParams textViewParams = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
String[] sentences = text.split("\n");
//calculate space width
textView.setText(" ");
textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int slice = textView.getMeasuredWidth();
//calculate each sentence
int[] sentencesWidth = new int[sentences.length];
//store max width
int maxWidth = 0;
for (int i = 0; i < sentences.length; i++) {
textView.setText(sentences[i]);
textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
sentencesWidth[i] = textView.getMeasuredWidth();
if (sentencesWidth[i] > maxWidth) {
maxWidth = sentencesWidth[i];
}
}
//add space after each sentence if necessary
for (int i = 0; i < sentences.length; i++) {
int spaceNum = (maxWidth - sentencesWidth[i]) / slice;
StringBuilder sb = new StringBuilder();
for (int k = 0; k < spaceNum; k++) {
sb.append(" ");
}
sentences[i] += sb.toString();
}
//rebuild String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sentences.length; i++) {
sb.append(sentences[i]);
if (i != sentences.length - 1) {
sb.append("\n");
}
}
return sb.toString();
}