2012-10-25 46 views
4

是否存在用於縮短Firemonkey中的按鈕或標籤文本的省略號/省略號例程? 例如,關:使用省略號縮短Firemonkey中的標籤

「C:\真的\真的\很長\很長的路徑\甚至更長的路徑名稱」「C:\真的\重......」「C:\真的\重...路徑名稱」

有各地的VCL程序,但它看起來就像找到Firemonkey的文字大小會更多地參與。

我使用Firemonkey 2德爾福XE3提前

謝謝...好,我創建了從麥克薩頓的意見笨重的過程。 它只在字符串的第一部分末尾添加省略號,但可以很容易地修改中間或末尾省略號位置。它還考慮到當前對象的字體大小和樣式。

用法是:

ShortenText(Button1, 'Start of text blah blah blah blah the END is here'); 

procedure ShortenText(anFMXObject: TTextControl; newText: string); 
var 
    aTextObj: TText; 
    shortenTo: integer; 
    modText: string; 
begin 
    if Length(newText) > 2 then 
    begin 
    modText:= newText+'...'; 
    aTextObj:=TText.Create(anFMXObject.Parent); 
    aTextObj.Font.SetSettings(anFMXObject.Font.Family, 
          anFMXObject.Font.Size, 
          anFMXObject.Font.Style); 
    aTextObj.WordWrap:= false; 
    aTextObj.AutoSize:= true; 
    aTextObj.Text:= newText; 
    // this next bit generates the change necessary to redraw the new text (bug in XE3 as of Oct 2012) 
    aTextObj.HorzTextAlign:= TTextAlign.taCenter; 
    aTextObj.HorzTextAlign:= TTextAlign.taLeading; 
    // now shorten the text: 
    shortenTo:= round((Length(modText)/aTextObj.Width)*anFMXObject.Width)-1; 
    modText:= LeftStr(modText, shortenTo-3)+'...'; 
    anFMXObject.Text:= modText; 
    FreeAndNil(aTextObj); 
    end; 
end; 

回答

1

自動調整大小設置爲True和總結設置爲False,那麼你可以簡單地讀取Width屬性,我建議用TText。

請注意,XE3中存在一個錯誤,並且在運行時將Text屬性設置爲不更新內容,所以您需要手動調用Realign(它是受保護的,因此您需要將TText繼承到子類得到這個工作)。