I have this string: "This is my very long String that wont fit on one line" And I need to split it into multiple lines so it'll fit where I need it.
Lets say theres only room for about 15 letters per line, then it should look like this:
"This is my very"
"long String"
"that wont fit"
"on one line"
I would like to split it into a List<String>
so I can do
for(String s : lines) draw(s,x,y);
Any help on how to do this would be appriciated!
The way I'm rendering the text is with Graphics.drawString()
This is what I've tried so far (horrible, I know)
String diaText = "This is my very long String that wont fit on one line";
String[] txt = diaText.trim().split(" ");
int max = 23;
List<String> lines = new ArrayList<String>();
String s1 = "";
if (!(diaText.length() > max)) {
lines.add(diaText);
} else {
for (int i = 0; i < txt.length; i++) {
String ns = s1 += txt[i] + " ";
if (ns.length() < 23) {
lines.add(s1);
s1 = "";
} else {
s1 += txt[i] + "";
}
}
}
int yo = 0;
for (String s : lines) {
Font.draw(s, screen, 70, 15 + yo, Color.get(-1, 555, 555,555));
yo += 10;
}