Suchen
Aktuell
- Aktuell
- Kommentare
- TagCloud
- Windows Phone 7: Silverlight und XNA
- 1267769400 1267769400 - Keine Kommentare
- Empfehlung: Microsoft Arc Keyboard
- 1266747660 1266747660 - 2 Kommentare
- C#: Outlook like Pop-up Alert
- 1264008720 1264008720 - Keine Kommentare
- Windows Mobile 7 - Verschiedene Versionen geplant
- 1264007340 1264007340 - Keine Kommentare
- Windows Media Center: Pixelfehler am rechten Rand
- 1260208800 1260208800 - Keine Kommentare
- Das Outlook Fenster kann nicht geöffnet werden #2
- 1259409180 1259409180 - 13 Kommentare
- Bis jetzt sind noch keine Kommentare geschrieben worden
Randnotiz
W3C
- Keine Kommentare
.Net Compact Framework: Texthöhe berechnen
Leider gibt es im .Net Compact Framework keine native Methode, um die benötigte Höhe eines Textes zu berechnen, welcher eine feste Breite hat. Graphics.MeasureString() funktioniert leider nur ohne Breitenangabe und liefert daher die Höhe und Breite einer Zeile zurück. Um dennoch an die Höhe eines Textes zu kommen, hab ich mir folgende Helfermethode geschrieben:
public static float GetTextHeight(int width, string text, Font font, Graphics graphics)
{
// Leerer String
if (string.IsNullOrEmpty(text))
return 0;
SizeF wordSize = new SizeF();
SizeF spaceSize = graphics.MeasureString(" ", font);
string[] parts = text.Split(' ');
float tmpWidth = 0;
int rows = 1;
for(int i = 0; i < parts.Length; i++)
{
wordSize = graphics.MeasureString(parts[i], font);
// Word passt noch in aktuelle Zeile
if (tmpWidth + wordSize.Width + spaceSize.Width <= width)
{
tmpWidth += wordSize.Width + spaceSize.Width;
}
// Neue Zeile anfangen
else
{
// Wort passt ohne Leerzeichen noch in alte Zeile
if (tmpWidth + wordSize.Width <= width)
{
if(i == parts.Length - 1)
continue;
tmpWidth = 0;
}
// Word in neue Zeile setzten
else
tmpWidth = wordSize.Width + spaceSize.Width;
rows++;
}
}
return rows * wordSize.Height;
}
Kommentare
- Bis jetzt sind noch keine Kommentare geschrieben worden



