2016-04-15 7 views
0

So habe ich zZ meine Zeichenkette, in der der Benutzer redigieren kann, was hineingelegt wird, solange es eine Zeichenkette ist. Bei kürzeren Texten funktioniert das gut, aber wenn der Text länger und länger wird, nimmt der Tooltip, den ich für die Stringliste verwende, zu viel Platz auf dem Bildschirm ein und lässt die Dinge hässlich aussehen. Also ich möchte eine neue Zeile für eine Stringliste für jede bestimmte Anzahl von Zeichen machen, damit es nicht unordentlich wird. Hier ist meine aktuellen Test, den ich für sie habe seit:Eine neue Zeile in der Stringliste erstellen, wenn die aktuelle Zeile zu viel Text enthält?

'Salamander specializes in disguising in the green woods, mainly in oak forests salamanders will hide and lure to wait for your loot, they will then jump out and do a sneak attack. Be prepared!' 

ich erhalten diese Informationen mit diesem:

lore.add(kitConfig.getString("kits." + kitName + ".description")); 
+0

Vielen Dank, ich werde das versuchen. –

Antwort

0

Jeder String innerhalb der Lore ArrayList<String> wird auf einer neuen Zeile angezeigt. Sie könnten eine Methode schreiben, die eine String in Segmente aufteilt und eine ArrayList zurückgibt, die die Teile enthält, die Sie dann als neue Überlieferung festlegen können. Hier ist ein Beispiel dafür, wie diese aussehen könnte:

public static ArrayList<String> splitLore(String text, int characters) { 
    ArrayList<String> lore = new ArrayList<String>(); // Create the ArrayList that will contain the lore lines 
    if (text.length() <= characters) { // If the line of text is short enough (doesn't need to be split)... 
     lore.add(text); // Add the entire line to the list and return it 
    } else { // If the line is longer and needs to be split into at least two lines... 
     int beginIndex = 0; // A "begin index" where each substring or segment will begin 
     while (beginIndex <= text.length() - 1) { // If the index is not larger than the last character index in the line of text... 
      lore.add(text.substring(beginIndex, Math.min(beginIndex + characters, text.length()))); // Add the segment 
      beginIndex += characters; 
      // This will also add any trailing segments at the end of the line that are shorter than the character limit 
     } 
    } 
    return lore; 
} 

Beispiel Nutzung:

String lore = "This is a long description of an item's lore that needs to be broken down" 
ItemMeta meta = // ... Get the ItemMeta object of an ItemStack 
meta.setLore(splitLore(lore, 10)); // Splits the single line into multiple ones containing 10 or less characters 

Diese Methode ist nicht sehr „smart“ und Worte abgeschnitten, Textzeilen zu schaffen, die nicht sehr schauen Sie recht oder sind schwer zu lesen.

Um die Lore angenehmer zu lesen, könnten Sie die Textzeile in "Wörter" aufteilen und dann versuchen, sie so zu gruppieren, dass keine Linie die "weiche" Zeichengrenze zu stark überschreitet. Die folgende Beispielmethode schneidet Wörter nicht auseinander, obwohl sie auch extrem lange Wörter, die mehr als eine ganze Zeile umfassen, nicht zwangsweise aufteilen wird.

public static ArrayList<String> splitLoreNicely(String text, int characters) { 
    ArrayList<String> lore = new ArrayList<>(); 
    String[] words = text.split(" "); // Get the "words" in the line of text by splitting the space characters 
    int wordsUsed = 0; // A counter for how many words have been placed in lines so far 
    while (wordsUsed < words.length) { // Repeat this process until all words have been placed into separate lines 
     String line = ""; // The line that will be added to the lore list 
     for (int i = wordsUsed; i < words.length; i++) { // For each remaining word in the array 
      if (line.length() + words[i].length() >= characters) { // If adding the next word exceeds or matches the character limit... 
       line += words[i]; // Add the last word in the line without a space character 
       wordsUsed++; 
       break; // Break out of this inner loop, since we have reached/exceeded the character limit for this line 
      } else { // If adding this word does not exceed or match the character limit... 
       line += words[i] + " "; // Add the word with a space character, continue for loop 
       wordsUsed++; 
      } 
     } 
     lore.add(line); // Add the line of text to the list 
    } 
    return lore; 
}