簡體   English   中英

WPF:文本旋轉和 alignment 問題

[英]WPF: Text rotation and alignment issues

我正在嘗試為 WPF 項目創建 label,該項目涉及具有不同前景色的部分和必須經歷 180 次旋轉的特殊字符。 我快到了,但有一些問題。

這是到目前為止的圖像:

在此處輸入圖像描述

這是生成它的 XAML :

       <Viewbox>
        <StackPanel Margin="5"
                    Background="White"
                    Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontFamily" Value="Trebuchet MS" />
                    <Setter Property="FontSize" Value="40" />
                </Style>
            </StackPanel.Resources>
            <TextBlock Margin="5,0,0,0"
                       VerticalAlignment="Bottom"
                       Foreground="Black"
                       Text="XXX" />
            <TextBlock VerticalAlignment="Bottom"
                       FontSize="50"
                       Foreground="Green"
                       Text="Δ">
                <TextBlock.LayoutTransform>
                    <RotateTransform Angle="180" />
                </TextBlock.LayoutTransform>
            </TextBlock>
            <TextBlock Margin="0,0,5,0"
                       VerticalAlignment="Bottom"
                       Foreground="Orange"
                       Text="YYY" />
        </StackPanel>
    </Viewbox> 

這樣做的問題是“XXX”和“YYY”之間的底部“特殊字符”已旋轉 180 度與“XXX”和“YYY”的底部不對齊。

因此,我嘗試重做 XAML ,其中我有一個TextBlock並且每個部分都被控制為Run

這是一張圖片:

在此處輸入圖像描述

這是修改后圖像的 XAML:

       <Viewbox>
        <StackPanel Margin="5"
                    Background="White"
                    Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontFamily" Value="Trebuchet MS" />
                    <Setter Property="FontSize" Value="40" />
                </Style>
            </StackPanel.Resources>
            <TextBlock Margin="5,0,0,0" VerticalAlignment="Bottom">
                <Run Foreground="Black" Text="XXX" />
                <Run FontSize="50"
                     Foreground="Green"
                     Text="Δ">
                    <!-- <Run.LayoutTransform>
                        <RotateTransform Angle="180" />
                    </Run.LayoutTransform> -->
                </Run>
                <Run Foreground="Orange" Text="YYY" />
            </TextBlock>
        </StackPanel>
    </Viewbox>

您會注意到,雖然底部的 alignment 問題現在沒問題,但這比第一次嘗試更糟糕,因為現在特殊字符與“XXX”和“YYY”之間存在間隙。 並且Run不支持轉換。

那么,任何人都可以幫我做我想做的事嗎? 也就是說,創建第一個圖像,但特殊字符與“XXX”和“YYY”垂直對齊。

謝謝。

您可以使用應用渲染轉換稍微調整垂直 position:

<TextBlock.LayoutTransform>
    <RotateTransform Angle="180" />
</TextBlock.LayoutTransform>
<TextBlock.RenderTransform>
    <TranslateTransform Y="3" />
</TextBlock.RenderTransform>

你在尋找類似BaselineAlignment的東西嗎?

這將使您的代碼如下所示:

<TextBlock Margin="5,0,0,0" VerticalAlignment="Center">
    <Run Foreground="Black" Text="XXX" BaselineAlignment="Center"/>
    <Run FontSize="50" Foreground="Green" Text="Δ" BaselineAlignment="Center" />
    <Run Foreground="Orange" Text="YYY" BaselineAlignment="Center" />
</TextBlock>

更新:

如果要旋轉三角形,可以通過在文本塊中添加<TextBlock>來實現。 所以它看起來像這樣:

<Run Foreground="Black" Text="XXX"  BaselineAlignment="Center"/>
    <TextBlock Magin="-12 0">
        <TextBlock.LayoutTransform>
            <RotateTransform Angle="180" />
        </TextBlock.LayoutTransform>
        <Run FontSize="50" Foreground="Green" Text="Δ" />
    </TextBlock>
    <Run Foreground="Orange" Text="YYY" BaselineAlignment="Center" />
</TextBlock>

這是一個混亂,但它的工作原理:

結果是這樣的:

無旋轉

...還有這個,帶有旋轉和邊距

帶旋轉和邊距

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM