Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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 :

1441053724-justified.png

 

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 !

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]

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.

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.