简体   繁体   English

Java Swing中的自动换行具有硬换行像素限制

[英]Word Wrap in Java Swing with Hard Wrap Pixel Limit

I am having issues implementing a word wrap in java in a swing enviroment. 我在swing环境中在Java中实现自动换行时遇到问题。 I have a hard limit in pixels that I can't go over. 我在像素上有一个严格的限制,无法逾越。 I feel like I am close as I have found a solution that wraps, but it lets the last word run over the limit before going to the next line. 我觉得自己已经接近了,因为我找到了一个可以包装的解决方案,但是它可以让最后一个单词超出限制,然后再转到下一行。

'ni' is a buffered image that this is all being drawn to. “ ni”是被全部吸引到的缓冲图像。

'theSettings' custom class that contains variables for the text. “ theSettings”自定义类,其中包含文本变量。

'theText' is the string that needs to be wrapped. “ theText”是需要包装的字符串。

This is what is getting passed to the method: 这就是传递给方法的内容:

FontMetrics fm = ni.createGraphics().getFontMetrics(theSettings.getFont());
//Get pixel width of string and divide by # of characters in string to get estimated width of 1 character
int charPixelWidth = fm.stringWidth(theText) / theText.length();
int charWrap = theSettings.getPixelsTillWrap() / charPixelWidth;
List<String> textList = testWordWrap(theText, charWrap);

I am using this method to test: 我正在使用此方法进行测试:

//Custom String Wrapper as StringUtils.wrap keeps cutting words in half
public static List<String> testWordWrap(String wrapMe, int wrapInChar) {
    StringBuilder sb = new StringBuilder(wrapMe);

    int count = 0;
    while((count = sb.indexOf(" ", count + wrapInChar)) != -1) {
        sb.replace(count, count + 1, "\n");
    }

    String toArray = sb.toString();
    String[] returnArray = toArray.split("\\n");

    ArrayList<String> returnList = new ArrayList<String>();
    for(String s : returnArray) {
        returnList.add(s);
    }

    return returnList;
}

Example in use: 使用示例: 在此处输入图片说明 在此处输入图片说明

I also replace images in the middle of the text, but the size of the image is exactly the size of the text that it is replacing; 我还在文本中间替换了图像,但是图像的大小恰好是它要替换的文本的大小。 so that shouldn't be an issue. 所以这不应该是一个问题。 The blue lines are bounding boxes that show where the wrap should be ending. 蓝线是边界框,显示包装应在何处结束。 It shows that the text continues to draw. 它显示文本继续绘制。 I need the function to wrap 'succeeded' to the next line; 我需要将“成功”包装到下一行的函数; as the wrap can't run over in any situation. 因为包装在任何情况下都不会溢出。

EDIT: The arrays of the image run as .toString(); 编辑:图像的数组作为.toString();运行.toString();

[Resolution: +|FT| [分辨率:+ | FT | if you succeeded, and matched a |EA|] 如果成功,并且匹配| EA |]

[Any: +|MT| [任何:+ | MT | when you gain any number, of |QT|] 当您获得任意数量的| QT |]

I was able to identify an answer. 我能够找到答案。 I split the string on spaces and then try adding each word one at a time. 我将字符串分割成空格,然后尝试一次将每个单词加一个。 If it is too long, jump to the next line. 如果太长,请跳至下一行。 Here it is over commented to help anyone else with this same issue. 在这里,它已过注释以帮助其他任何人解决同样的问题。 Cheers! 干杯!

//@Author Hawox
//split string at spaces
//for loop checking to see if adding that next word moves it over the limit, if so go to next line; repeat
public static List<String> wordWrapCustom(String wrapMe, FontMetrics fm, int wrapInPixels){
    //Cut the string into bits at the spaces
    String[] split = wrapMe.split(" ");

    ArrayList<String> lines = new ArrayList<String>(); //we will return this, each item is a line
    String currentLine = ""; //All contents of the currentline

    for(String s : split) {
        //Try to add the next string
        if( fm.stringWidth(currentLine + " " + s) >= wrapInPixels ) {
            //Too big
            if(currentLine != "") { //If it is still bigger with an empty string, still add at least 1 word
                lines.add(currentLine); //Add the line without this string 
                currentLine = s; //Start next line with this string in it
                continue;
            }
        }
        //Still have room, or a single word that is too big
        //add a space if not the first word
        if(currentLine != "")
            currentLine = currentLine + " ";

        //Append string to line
        currentLine = currentLine + s;
    }
    //The last line still may need to get added
    if(currentLine != "") //<- Should not get called, but just in case
        lines.add(currentLine);

    return lines;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM