Suchen

Suche

Aktuell

  • Aktuell
  • Kommentare
  • TagCloud
  • Bis jetzt sind noch keine Kommentare geschrieben worden
 .Net   Suche   ASP.Net   Asp.Net   Beta   Buchtipp   C#   CentOS   CoD   DateTime   DiceFight   Funktion   HTML   HowTo   HttpWebRequest   IIS7   JSON   Konfiguration   LCD   Lighttpd   Linux   Media   Microsoft   Mobile   Mono   MySQL   Oracle   Outlook   PHP   PostgreSQL   Programme   Quake3   RC   RealURL   SMTPE   SVN   Server   Snippet   TYPO3   Timeout   Treiber   Tutorial   USB   Vista   WebClient   Windows   bauer-martin.com   bm_chart   ffmpeg   x64 

Randnotiz

W3C

Valid XHTML Valid CSS

.Net - 24.07.2009 07:41 - Alter: 231 days
- Keine Kommentare

.Net Compact Framework: Texthöhe berechnen

Tags: .Net, Compact Framework, Text

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



Blink del.icio.us Digg Furl Google Simpy Spurl Technorati Yahoo

Kommentare

  • Bis jetzt sind noch keine Kommentare geschrieben worden

Kommentar schreiben

Ins Gästebuch eintragen
CAPTCHA Bild zum Spamschutz