簡體   English   中英

Java Swing中的自動換行具有硬換行像素限制

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

我在swing環境中在Java中實現自動換行時遇到問題。 我在像素上有一個嚴格的限制,無法逾越。 我覺得自己已經接近了,因為我找到了一個可以包裝的解決方案,但是它可以讓最后一個單詞超出限制,然后再轉到下一行。

“ ni”是被全部吸引到的緩沖圖像。

“ theSettings”自定義類,其中包含文本變量。

“ theText”是需要包裝的字符串。

這就是傳遞給方法的內容:

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);

我正在使用此方法進行測試:

//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;
}

使用示例: 在此處輸入圖片說明 在此處輸入圖片說明

我還在文本中間替換了圖像,但是圖像的大小恰好是它要替換的文本的大小。 所以這不應該是一個問題。 藍線是邊界框,顯示包裝應在何處結束。 它顯示文本繼續繪制。 我需要將“成功”包裝到下一行的函數; 因為包裝在任何情況下都不會溢出。

編輯:圖像的數組作為.toString();運行.toString();

[分辨率:+ | FT | 如果成功,並且匹配| EA |]

[任何:+ | MT | 當您獲得任意數量的| QT |]

我能夠找到答案。 我將字符串分割成空格,然后嘗試一次將每個單詞加一個。 如果太長,請跳至下一行。 在這里,它已過注釋以幫助其他任何人解決同樣的問題。 干杯!

//@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