2012-03-20 88 views
7

我期待在TextBlock上獲得特定行爲,以便其高度僅包含大寫字母的高度(從基線到頂部減去「上升高度」) 。請參閱Wikipedia中的圖片Sphinx,瞭解我的意思。另外下面的圖片可能表明我更喜歡什麼。TextBlock與大寫字母一樣大(忽略字體ascender/descender)

Sample enter image description here

我不是專門找了一個純粹XAML解決方案(可能是不可能的),所以C#代碼隱藏(轉換器)也可以。

這是XamlPad在XamlPad中用於生成上圖中左側的A的示例。

<TextBlock Text="A" Background="Aquamarine" FontSize="120" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
+0

您應該使用MeasureString或一些類似的技術。看看這個問題http://stackoverflow.com/questions/824281/wpf-equivalent-to-textrenderer – 2012-03-20 14:16:01

回答

1

更新時間:

如果我理解正確的,有一些小技巧,我知道這一點,

你可以用的RenderTransform通常是最有效的方式Scale它;

<TextBlock Text="Blah"> 
    <TextBlock.RenderTransform> 
    <CompositeTransform ScaleY="3"/> 
    </TextBlock.RenderTransform> 
</TextBlock> 

或者你可以嵌入在ViewboxTextBlock「放大」例如,如果你設置硬高度值在網格行喜歡的文本以適應其容器的邊界;

<Grid> 
<Grid.RowDefinitions> 
<RowDefinition Height="120"/> 
<RowDefinition Height="120"/> 
</Grid.RowDefinitions> 
<Viewbox VerticalAlignment="Stretch" Height="Auto"> 
     <!-- The textblock and its contents are 
     stretched to fill its parent --> 
     <TextBlock Text="Sphinx" /> 
</Viewbox> 
<Viewbox Grid.Row="2" VerticalAlignment="Stretch" Height="Auto"> 
     <!-- The textblock and its contents are 
     stretched to fill its parent --> 
     <TextBlock Text="Sphinx2" /> 
</Viewbox> 

或者您可以將FontSize綁定到Container元素,例如;

<Grid x:Name="MyText" Height="120"> 
<TextBlock FontSize="{Binding ElementName=MyText, Path=Height}" Text="Sphinx" /> 
</Grid> 

它們可能會呈現出您所追求的效果?

+0

嗯......不是真的。我在第一個XAML中看不到任何效果。第二個有一個額外的「.Value」,但仍然沒有這樣做。不管怎麼說,還是要謝謝你。我添加了新的圖像來進一步解釋。我越來越接近使用自定義轉換器和多重綁定的方式。雖然這並不漂亮。我正在尋找一個簡單的解決方案。 – wpfwannabe 2012-03-20 17:35:36

+0

那麼我試圖做一個編輯,它不讓我保存說帖子不能包含該內容?這是一個奇怪的事情,沒有解釋,但我認爲你想要做的也許是使用GlyphTypeFace找到你lineheight的值,並設置它沒有你的行間隙(領先)將看起來更多,如果我有更多的時間,現在我好奇好奇從來沒有理由像以前那樣混淆字體。 – 2012-03-20 18:59:26

+0

謝謝!畢竟,我以我想要的方式工作。如果沒有人提出解決方案作爲練習,我會在幾天後發佈自己的答案。 – wpfwannabe 2012-03-20 19:51:38

4

你可以嘗試使用屬性LineStackingStrategy =「BlockLineHeight」和LineHeight屬性上的Converter以及TextBlock高度上的轉換器。 該轉換器的示例代碼

// Height Converter 
public class FontSizeToHeightConverter : IValueConverter 
{ 
    public static double COEFF = 0.715; 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return (double)value * COEFF; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
// LineHeightConverter 
public class FontSizeToLineHeightConverter : IValueConverter 
{ 
    public static double COEFF = 0.875; 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return double.Parse(value.ToString()) * COEFF; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

上轉換器所使用的係數取決於所使用的字體家族(基線和LineSpacing):

<TextBlock Text="ABC" Background="Aqua" LineStackingStrategy="BlockLineHeight" 
FontSize="{Binding ElementName=textBox1, Path=Text}" 
FontFamily="{Binding ElementName=listFonts, Path=SelectedItem}" 
Height="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Mode=OneWay, Converter={StaticResource FontSizeToHeightConverter1}}" 
LineHeight="{Binding RelativeSource={RelativeSource Self}, Path=FontSize, Converter={StaticResource FontSizeToLineHeightConverter}}"/> 

sample with params Coeff = 0.7

最好的解決辦法是找到如何根據FontFamily的參數Baseline和LineSpacing計算Coeff。 在此示例(Segeo UI)中,高度Coeff = 0.715,LineHeight = 0,875 * FontSize。