2011-04-07 48 views
0

我試圖根據可用空間動態縮短標籤。我用FormattedText來確定候選串衡量標籤文本的長度

formattedText = new FormattedText(candidateString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, FontSize = fontSize, Brushes.Black); 

我應該能夠檢索包含文本標籤字體的長度;但我仍然遇到障礙:

  • 標籤包含FontFamily但不是字體。
  • 我不能創建的FontFamily一個新的字體,因爲我不能讓一個單一的FontFamily名
    Typeface t = new Typeface(theLabel.FontFamily.FamilyNames.?, theLabel.FontStyle, theLabel.FontSize, theLabel.FontStretch);

這似乎是它應該是簡單的:我想在標籤中使用的字體(指向屏幕)。如何創建一個用於測量我的字符串長度的字體?有沒有更簡單的方法來實現這一點?我在這裏錯過了什麼?

謝謝。

+1

你有沒有考慮到使用的TextBlock和財產的TextTrimming代替? – 2011-04-07 01:28:52

回答

1

這不行?

Typeface t = new Typeface(theLabel.FontFamily,theLabel.FontStyle,theLabel.FontWeight,theLabel.FontStretch, new FontFamily("Arial")); 

,或者你可以嘗試用

theLabel.FontFamily.GetTypefaces(); 
+0

這確實有用;而後備字體(最後一個參數)是可選的,所以它不需要任何硬編碼值。謝謝。 – 2011-04-07 14:53:12