2015-11-11 102 views
1

我需要更改我的UWP应用程序中文本的显示顺序,但不幸的是我找不到任何直接的解决方案。如何在UWP/WinRT中垂直文本显示中的文本

WinRT中的文本块不支持此属性,至少我无法从MSDN中找到有关此功能的任何信息。我找到了一个解决方案,我需要创建一个“新”文本块控件,它支持垂直顺序的文本显示,但解决方案是针对silverlight的,所以我正在研究它是否有效。

这是文本块如何正常工作: enter image description here

这是怎么了文本块,我想它的工作:

enter image description here

我知道有一种方式,只设置了宽度和文字包装的东西,但它只适用于一定的屏幕尺寸&分辨率,这意味着在其他屏幕下的文字将无法正常显示

任何提示将不胜感激。

+0

@yms嘿,我刚刚更新我的问题,你可以只看着它再次,如果你会的。非常感谢你 –

+0

将宽度设置得有些微小,你的字母会自行显示。这至少保证工作。 – Croll

+2

可能重复[垂直文本在Wpf TextBlock](http://stackoverflow.com/questions/349875/vertical-text-in-wpf-textblock) –

回答

0

编辑 - UWP verison与用户控制

VerticalTextBlock - XAML

<UserControl 
    ... 
    > 
    <TextBlock x:Name="TheTextBlock"/> 
</UserControl> 

使用和试验 - - XAML

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <TextBlock x:Name="a" Text="ASD"></TextBlock> 
    <local:VerticalTextBlock x:Name="b" Text="{Binding ElementName=a, Path=Text}" /> 
    <local:VerticalTextBlock x:Name="c" Text="{Binding ElementName=b, Path=Text}" /> 
    <TextBlock x:Name="d" Text="{Binding ElementName=c, Path=Text}"></TextBlock> 
    <TextBlock TextAlignment="Center" HorizontalAlignment="Left"> 
     <Run Text="A"/> 
     <LineBreak/> 
     <Run Text="S"/> 
     <LineBreak/> 
     <Run Text="D"/> 
     <LineBreak/> 
     <Run Text="A"/> 
     <LineBreak/> 
     <Run Text="S"/> 
     <LineBreak/> 
     <Run Text="D"/> 
    </TextBlock> 
</StackPanel> 
后面

public partial class VerticalTextBlock : UserControl 
{ 

    public VerticalTextBlock() 
    { 
     InitializeComponent(); 
    } 

    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", 
      typeof(string), 
      typeof(VerticalTextBlock), 
      new PropertyMetadata(string.Empty, textChangeHandler)); 

    private static void textChangeHandler(
     DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var prop = d as VerticalTextBlock; 
     var textBlock = prop.TheTextBlock; 
     var str = (e.NewValue as string); 
     textBlock.Inlines.Clear(); 
     for (int i = 0; i < str.Length-1; i++) 
     { 
      textBlock.Inlines.Add(new Run() { Text = str[i] + Environment.NewLine }); 
     } 
     textBlock.Inlines.Add(new Run() { Text = str[str.Length-1].ToString()}); 
    } 
} 

VerticalTextBlock代码

原来的答案 - 没有注意到它的UWPWPF

你让我感兴趣,我只是在做Android的这一点,所以有一些解决方案,将工作,但我决定尝试自定义控件在XAML

延长 TextBlock

public partial class VerticalTextBlock : TextBlock 
{ 
    public VerticalTextBlock() 
    { 
     InitializeComponent(); 
    } 

    new public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    new public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", 
      typeof(string), 
      typeof(VerticalTextBlock), 
      new PropertyMetadata(string.Empty, textChangeHandler)); 

    private static void textChangeHandler(
     DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var prop = d as VerticalTextBlock; 
     var str = (e.NewValue as string); 
     var inlines = str.Select(x => new Run(x + Environment.NewLine)); 
     prop.Inlines.Clear(); 
     prop.Inlines.AddRange(inlines); 
    } 
} 

使用

<local:VerticalTextBlock Text="AABBCCDDEEFF" /> 
+0

请记住,这是UWP,**不是** WPF。 –

+0

感谢您的更正@JustinXL – kirotab

+0

这是一个很好的解决方案。上面的解决方案不起作用,所以基本上我认为我应该创建一个自定义文本块 –

1

要获得UWP TR一个 “真正的” 垂直文本Ÿ如下:

<TextBlock Text="Rotated Text" 
      FontSize="18" 
      Foreground="Black"> 
    <TextBlock.RenderTransform> 
     <RotateTransform Angle="-90" /> 
    </TextBlock.RenderTransform> 
</TextBlock>