Methode die Text kürzt?
Methode die Text kürzt?
Habt ihr zufällig schon eine Methode die aus einem langen String
NewsFeed Hallo Welt, NewsFeed Hallo Erde, NewsFeed Hallo Mars
je nach Platz in einem Label
NewsF. Hall. Wel., NewsF. Hall. Erd., NewsF. Hall. Mar...
macht?
Also den Text nach jedem Wort kürzt bis der Platz reicht, nicht das typische "..." am Ende was sonst kommt.
Doppelte Arbeit muss ja nicht sein
NewsFeed Hallo Welt, NewsFeed Hallo Erde, NewsFeed Hallo Mars
je nach Platz in einem Label
NewsF. Hall. Wel., NewsF. Hall. Erd., NewsF. Hall. Mar...
macht?
Also den Text nach jedem Wort kürzt bis der Platz reicht, nicht das typische "..." am Ende was sonst kommt.
Doppelte Arbeit muss ja nicht sein
Das wär der absolute worst-casebodo hat geschrieben:H.f. M.e.e. sdf.er.we.er.e.wer. usw
Mal auf die Schnelle:
Code: Alles auswählen
public static String truncateText(FontMetrics metrics, int width, String txt) {
if(txt == null) {
return null;
}
final int actWidth = metrics.stringWidth(txt);
if(actWidth <= width) {
return txt;
}
StringBuffer buffer = new StringBuffer(txt.length());
String[] parts = txt.split("\\s");
int minLength = 0;
for(int i=0; i<parts.length; i++) {
if(parts[i] != null) {
minLength = Math.max(minLength, parts[i].trim().length());
}
}
for(int i=0; i<parts.length; i++) {
if(parts[i] != null) {
String elem = parts[i].trim();
if(elem.length() >= minLength ) {
if(elem.endsWith(",")) {
buffer.append(elem.substring(0, elem.length()-3));
buffer.append(".,");
} else {
buffer.append(elem.substring(0, elem.length()-2));
buffer.append(".");
}
} else {
buffer.append(elem);
}
if(i <= parts.length) {
buffer.append(" ");
}
}
}
return truncateText(metrics, width, buffer.toString());
}
Ich finde, daß sich das Kürzen nicht lohnt.platin(x) hat geschrieben:Du sieht's aber nur wenn du einige Feeds drin hast oder das Fenster sehr klein ist. Falls zu viele drin sind, wird nichts gekürzt.
Weil:
- 1. fällt das eh niemandem auf
- 2.
at java.lang.String.substring(Unknown Source)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:100)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.util.IOToolkit.truncateText(IOToolkit.java:117)
at newsfeedplugin.ui.NewsFeedStatusBar$StatusTextWrapper.update(NewsFeedStatusBar.java:338)
at newsfeedplugin.ui.NewsFeedStatusBar$StatusTextWrapper.add(NewsFeedStatusBar.java:310)
at newsfeedplugin.ui.NewsFeedStatusBar$StatusTextWrapper.access$300(NewsFeedStatusBar.java:294)
at newsfeedplugin.ui.NewsFeedStatusBar.propertyChange(NewsFeedStatusBar.java:150)
at newsfeedplugin.core.NewsFeedObject.firePropertyChange(NewsFeedObject.java:527)
at newsfeedplugin.core.NewsFeedObject.setState(NewsFeedObject.java:304)
at newsfeedplugin.core.NewsFeedObject.downloadAndParseXml(NewsFeedObject.java:325)
at newsfeedplugin.ui.NewsFeedChooserPanel$9.run(NewsFeedChooserPanel.java:258)