2011-04-18 28 views
4

嗨 我有一個問題,調整文本的長度,以便它適合形式的長度。 在我的表格中,我放置了一個負責顯示一些長文本的標籤。然而,文本可能會很長,以致不適合形成。這就是爲什麼我想縮短這段文字並將...添加到最後。我怎樣才能確定一個字符串將適合一定的寬度多少?

的問題是,我不知道如何有效地計算出最大lenght這將適應形式的字符串。到目前爲止,我只設法檢查給定的文本是否太長(我使用Graphics類中的方法MeasureString)。 什麼是計算最適合形式的最大子字符串的最佳方法(子字符串width < form.Width)?

+7

有那麼一刻我以爲這是一個玩笑後,詢問一條繩子有多長... – Oded 2011-04-18 20:18:22

+7

也許你只是需要設置['Label.AutoEllipsis'] (http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autoellipsis.aspx)屬性。 – 2011-04-18 20:19:37

+1

那麼你必須考慮到字母不具有相同的大小(在許多字體中,除了等寬字體):例如'W'並不像'我'那麼小,所以你可以通常不會認爲一個字母數目相同的單詞具有相同的大小。 – 2011-04-18 20:20:27

回答

1

我知道的唯一可靠的方法是縮短你的字符串,直到它適合你可以使用MeasureString()進行檢查,不幸的是沒有反向方法(給定一個寬度,這個字符串有多大可以適合),所以你打算必須自己建立。

+3

如果你對字符串進行二分搜索,它不應該是很難弄清楚它的長度將適合什麼形式。 – Gabe 2011-04-18 20:21:19

1

自從我今天遇到它以來,我對此問題有了一個解決方案。我的ToolStripStatusLabel被設置爲自動調整大小,但超過了父控件的寬度,因此隱藏自身。我的解決方案專門用於ToolStripStatusLabel控件。由於這種情況,此控件不提供MaxWidth屬性或AutoEllipsis屬性。它也不會爲我生成一個Graphics對象,因爲它不是控件系列的一部分。此方法可以更改爲與兩個Control對象一起使用,除參數外沒有太多變化。我的選擇是相當有限的,所以我製作這個方法:

/// <summary> 
/// If a child ToolStripStatusLabel is wider than it's parent then this method will attempt to 
/// make the child's text fit inside of the parent's boundaries. An ellipsis can be appended 
/// at the end of the text to indicate that it has been truncated to fit. 
/// </summary> 
/// <param name="child">Child ToolStripStatusLabel</param> 
/// <param name="parent">Parent control where the ToolStripStatusLabel resides</param> 
/// <param name="appendEllipsis">Append an "..." to the end of the truncated text</param> 
public static void TruncateChildTextAccordingToControlWidth(ToolStripStatusLabel child, Control parent, bool appendEllipsis) 
{ 
    //If the child's width is greater than that of the parent's 
    if(child.Size.Width > parent.Size.Width) 
    { 
     //Get the number of times that the child is oversized [child/parent] 
     decimal decOverSized = (decimal)(child.Size.Width)/(decimal)(parent.Size.Width); 

     //Get the new Text length based on the number of times that the child's width is oversized. 
     int intNewLength = (int)(child.Text.Length/(2M * decOverSized)); //Doubling as a buffer (Magic Number). 

     //If the ellipsis is to be appended 
     if(appendEllipsis) //then 3 more characters need to be removed to make room for it. 
      intNewLength = intNewLength - 3; 

     //If the new length is negative for whatever reason 
     if(intNewLength < 0) 
      intNewLength = 0; //Then default it to zero 

     //Truncate the child's Text accordingly 
     child.Text = child.Text.Substring(0, intNewLength); 

     //If the ellipsis is to be appended 
     if(appendEllipsis) //Then do this last 
      child.Text += "..."; 
    } 
} 

我將是第一個指出的是,我做了一件可怕的事情,其中​​包括一個神奇的數字!我很抱歉,但2M是一個緩衝區。我找不到一個更好的方法來做到這一點,當/如果我找到一種方法,我一定會回來,並在這裏更新它。我需要這個神奇數字,因爲我計算的百分比是準確的,但爲了適合父母內部的孩子控制,我需要大約兩倍的回報。

我敢肯定,這取決於標籤字體大小和其他因素不會完全準確,但嘿它的一個開端。

0

您可以通過字寫的文字字,同時向前移動光標點,直到它到達行的末尾(表格的寬度)。我有類似的問題,但我正在繪製表格上的文字,我改變了我的代碼以適應您的需要。

private bool WriteWord(string word) 
{ 
    Size size = TextRenderer.MeasureText(word + " ", Font); 
    if (cursor.X + size.Width > Width) 
    { 
     // you reached the end of the line 
     return false; 
    } 
    label1.Text += word + " "; // add the word to label. 
    cursor.X += size.Width; 
    return true; 
} 
Point Cursor; 
private string FindMaxSubstring(string text) 
{ 
    string[] tokens = text.Split(new string[] { " "}); 
    string substring =""; 
    Cursor.X = 0; 
    foreach (var t in tokens) 
    { 
     if (!string.IsNullOrEmpty(t.Trim())) 
     { 
      if (!WriteWord(t)) 
       return substring; 
      substring += t; 
     } 
    } 
} 
0

設置標籤,以真正的AutoSize財產,那麼你的文本拆分令牌,並將它們添加由令牌的令牌至label.Text直到label.Width > Width.

1

我用這個代碼,通過剝離,使我的狀態欄的文字配合字符串開頭的字符。也許你可以將它修改爲您的需求

 status.Text = status.Text + " | " + newText; 
     while (TextRenderer.MeasureText(status.Text, status.Font).Width > status.Width) 
      status.Text = status.Text.Substring(1); 
相關問題