2010-03-23 3 views
3

.NET을 사용하여 제한된 공간에 문자열을 그립니다. 문자열을 최대한 크게하고 싶습니다. 문자열에 더 많은 선을 긋지 않아도 문제가 없습니다 (직사각형 안에 있으면). 이제 문제는 : .NET에서 단어 중간에 다른 줄에있는 문자열을 끊기를 원하지 않습니다. 예를 들어 "Test"문자열은 큰 글꼴로 한 줄에 인쇄됩니다. String "Testing"은 한 줄에 작은 글꼴 (한 줄에는 "Testi"가 아니고 다른 줄에는 "ng"가 아닌) 한 줄로 인쇄해야하며 문자열 "Test Test"는 상당히 큰 글꼴로 두 줄에 인쇄해야합니다..NET에서 줄을 줄 바꿈 할 때 단어 분리 사용 안 함 DrawString

아무도 내 단어를 손상시키지 않도록 .NET을 제한하는 방법에 대한 아이디어가 있습니까?

나는 현재이 같은 코드를 사용하고 있습니다 :

 internal static void PaintString(string s, int x, int y, int height, int maxwidth, Graphics g, bool underline) 
    { 
     FontStyle fs = FontStyle.Bold; 
     if (underline) 
      fs |= FontStyle.Underline; 
     Font fnt = new System.Drawing.Font("Arial", 18, fs); 
     SizeF size = g.MeasureString(s, fnt, maxwidth); 
     while (size.Height > height) 
     { 
      fnt = new System.Drawing.Font("Arial", fnt.Size - 1, fs); 
      size = g.MeasureString(s, fnt, maxwidth); 
     } 
     y = (int)(y + height/2 - size.Height/2); 
     g.DrawString(s, fnt, new SolidBrush(Color.Black), new Rectangle(x, y, maxwidth, height)); 
    } 

답변

0

당신은 문자열의 길이/크기에 따라 제어 크기의 크기를 조정할 수 있습니다. 이렇게하면 문자열이 한 줄에 들어갈 수 있습니다.

+1

하지만 내가 크기를 조정하지 않으려는 고정 된 공간이있다. 텍스트 크기를 줄이거 나 줄여서 텍스트에 맞춰야합니다. – JanHudecek

0

올바른 대답 인 것으로 보입니다. 나는 당신을 위해 모든 것을 할 수있는 프레임 워크 메서드에 대한 단일 호출이 있다고 생각하지 않는다. winform에서 버튼과 텍스트를 렌더링하는 다른 옵션은 ButtonRenderer 및 TextRenderer 클래스를 살펴 봐야합니다. DrawText 또는 MeasureString을 호출 할 때 WorkBreak, SingleLine 또는 Ellipse truncation을 지정할 수있는 TextFormatFlags를 지정할 수도 있습니다.

1

하면 문자열에서 가장 긴 단어를 찾아 그것을 한 줄에 맞는 보장하기 위해 MeasureString를 사용

internal static void PaintString(string s, int x, int y, int maxHeight, int maxWidth, Graphics graphics, bool underline) 
{ 
    FontStyle fontStyle = FontStyle.Bold; 
    if (underline) 
    { 
     fontStyle |= FontStyle.Underline; 
    } 

    var longestWord = Regex.Split(s, @"\s+").OrderByDescending(w => w.Length).First(); 
    using (var arial = new FontFamily("Arial")) 
    using (var format = new StringFormat(StringFormatFlags.LineLimit)) // count only lines that fit fully 
    { 
     int fontSize = 18; 
     while (fontSize > 0) 
     { 
      var boundingBox = new RectangleF(x, y, maxWidth, maxHeight); 
      using (var font = new Font(arial, fontSize, fontStyle)) 
      { 
       int charactersFittedAll, linesFilledAll, charactersFittedLongestWord, linesFilledLongestWord; 
       graphics.MeasureString(s, font, boundingBox.Size, format, out charactersFittedAll, out linesFilledAll); 
       graphics.MeasureString(longestWord, font, boundingBox.Size, format, out charactersFittedLongestWord, out linesFilledLongestWord); 

       // all the characters must fit in the bounding box, and the longest word must fit on a single line 
       if (charactersFittedAll == s.Length && linesFilledLongestWord == 1) 
       { 
        Console.WriteLine(fontSize); 
        graphics.DrawString(s, font, new SolidBrush(Color.Black), boundingBox, format); 
        return; 
       } 
      } 

      fontSize--; 
     } 

     throw new InvalidOperationException("Use fewer and/or shorter words"); 
    } 
}