Posted August 31, 201510 yr Hey there, I'm using Forge 11.14.3.1502. I'd like to justify text in Minecraft (not left or right, justified), and it seems that it's not possible due to char width variation. I created a function that breaks a String into lines (visually), from char width : public void drawQuestText() { String currentLine = ""; int currentWidth = 0, offset = 5; int index = 0; while (index < this.string.length()) { if ((currentWidth + this.fontRendererObj.getCharWidth(this.string.charAt(index))) <= this.maxPixelsPerLine && this.string.charAt(index) != '\n') { currentLine += this.string.charAt(index); currentWidth += this.fontRendererObj.getCharWidth(this.string.charAt(index)); ++index; } else { this.fontRendererObj.drawString(I18n.format(currentLine, new Object[0]), (this.width - this.bgWidth) / 2 + 5, (this.height - this.bgHeight) / 2 + offset, 4210752); currentLine = ""; currentWidth = 0; offset += this.fontRendererObj.FONT_HEIGHT; if (this.string.charAt(index) == '\n') ++index; } } if (currentWidth != 0) this.fontRendererObj.drawString(I18n.format(currentLine, new Object[0]), (this.width - this.bgWidth) / 2 + 5, (this.height - this.bgHeight) / 2 + offset, 4210752); } Result : where this.string could be any string/text you want and this.maxPixelsPerLine is equal to 238 pixels. this.bgWidth and this.bgHeight are equals to 248 and 166. This is not really efficient. Do you guys have any ideas to accomplish this ? Squirrel ! Squirrel ! Squirrel !
August 31, 201510 yr To be honest, I would just keep adding spaces to make the individual words move "all in one piece", but that is time consuming and inefficient. You need to get the width of the GUI and the width / length of the string being drawn. Test if the length of the string drawn plus the previous drawn string lengths (the length of the words and spaces before it on the same line) is greater than the GUI width - if not, draw it - else, move it to the next line. That is a basic idea of what you've got to do - hope that helps you out. Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]
September 2, 201510 yr I imagine there already exist some really nice utility classes that can do this if you can find the right library... or you can even use the built-in Minecraft code: String text = "Your text\nAnother line of text."; String[] temp = text.split("\\\\n"); // split the text on new-lines List<String> desc = new ArrayList<String>(); // this will store the resulting wrapped text for (String s : temp) { desc.addAll(fontRendererObj.listFormattedStringToWidth(s, 101)); // whatever line length you want, here I use 101 desc.add(""); // this will be an empty line, simulating a paragraph break or new-line } I use that in combination with a scroll bar for lengthy texts and it works great, taking into account things like individual character width. It's still not exactly what you want if you really want 'justified' text like you'd find in a text editor, as it doesn't pad characters to fit shorter strings to the width, but you can view the code and perhaps use that as a starting point. http://i.imgur.com/NdrFdld.png[/img]
September 2, 201510 yr Because "Lorem Ipsum" is too mainstream. 1.7.10 is no longer supported by forge, you are on your own.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.