Skip to content

TextUtil ​

com.jsmacrosce.jsmacros.util.TextUtil

Version-compatible raw formatted-text style lookup using font pixel width; this is not a global library.

js
const TextUtil = Java.type("com.jsmacrosce.jsmacros.util.TextUtil");  
const font = Client.getMinecraft().font;  
if (font) {
 const text = Chat.createTextHelperFromString("Hello").getRaw();  
 const style = TextUtil.componentStyleAtWidth(font, text, 0);  
 Chat.log(style !== null);  
}

Methods ​

componentStyleAtWidth ​

static componentStyleAtWidth(Font font, FormattedText text, int pixelX): Style#
Returns the raw style of the first formatted character whose accumulated font width exceeds the pixel budget, or null if traversal finishes without crossing it. The budget is measured from the start of the text; an exact boundary does not select the character ending at that boundary. This visits formatted text runs, not wrapped lines or screen hit regions. From Minecraft 1.21.11, a negative budget returns null immediately; earlier targets delegate it to Minecraft's splitter.

Parameters:

  • font: non-null font used to measure character widths
  • text: non-null formatted text to visit
  • pixelX: width budget in pixels from the start of the text
Returns: raw style at the first width overflow, or null if text traversal finishes