原文:WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能。另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好了。
由于本人比较懒,没有重新做,文字竖排和字符间距主要是通过新建继承自StackPanel的FormatedText类逐字符添加StrokeableLabel做的,竖排是用的StackPanel.Orientation来设置的,字符间距主要用的StrokeableLabel.Margin。
对于StrokeableLabel只是添加了设计时支持,其他没有改变,如果对StrokeableLabel不了解请看http://blog.csdn.net/springberlin/article/details/45699625
FormatedText有如下新添属性:
StretchSize:字符间距
TextOrientation:文字排版
下面是效果图
XMAL配置:
<Window x:Class="StrokeableLabelTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpflib="clr-namespace:BLCTClassLibrary.WpfLib;assembly=BLCTClassLibrary.WpfLib"
Title="MainWindow" Height="422" Width="579">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" >
<Label Margin="10" Content="以下是StrokeableLabel类(相对轻量级)"/>
<wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50"/>
<wpflib:StrokeableLabel Text="测试文本" Fill="Yellow" Stroke="Red" StrokeThickness="0.7" FontWeight="DemiBold" FontSize="50">
<wpflib:StrokeableLabel.Effect>
<DropShadowEffect Color="Black" BlurRadius="15" RenderingBias="Quality" Direction="290" ShadowDepth="5" Opacity="1" />
</wpflib:StrokeableLabel.Effect>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Text="测试文本" Fill="White" StrokeThickness="2" FontWeight="Bold" FontSize="50">
<wpflib:StrokeableLabel.Stroke>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="Blue" Offset="0.2"/>
<GradientStop Color="Brown" Offset="0.3"/>
<GradientStop Color="PowderBlue" Offset="0.7"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</wpflib:StrokeableLabel.Stroke>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Text="测试文本" Stroke="red" StrokeThickness="2" FontWeight="Bold" FontSize="50">
<wpflib:StrokeableLabel.Fill>
<ImageBrush ImageSource="/StrokeableLabelTest;component/Images/20085385922474_2.jpg" />
</wpflib:StrokeableLabel.Fill>
</wpflib:StrokeableLabel>
<wpflib:StrokeableLabel Fill="Transparent" FontSize="50" FontWeight="Light" StrokeThickness="8" Text="测试文本" >
<wpflib:StrokeableLabel.Stroke>
<ImageBrush ImageSource="/StrokeableLabelTest;component/Images/05.jpg" />
</wpflib:StrokeableLabel.Stroke>
</wpflib:StrokeableLabel>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Vertical" >
<TextBlock Margin="10" Text="以下是FormatedText类(在StrokeableLabel的基础上可以使文字竖排,可以控制字符间距)" TextWrapping="WrapWithOverflow" />
<wpflib:FormatedText StretchSize="-5" TextOrientation="Vertical" HorizontalAlignment="Center" Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50"/>
<wpflib:FormatedText StretchSize="-10" HorizontalAlignment="Center" Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50"/>
<wpflib:FormatedText StretchSize="20" HorizontalAlignment="Center" Text="测试文本" Fill="Yellow" Stroke="Black" StrokeThickness="0.3" FontWeight="Bold" FontSize="50">
<wpflib:FormatedText.Effect>
<DropShadowEffect Color="Black" BlurRadius="15" RenderingBias="Quality" Direction="290" ShadowDepth="5" Opacity="1" />
</wpflib:FormatedText.Effect>
</wpflib:FormatedText>
</StackPanel>
</Grid>
</Window>
库文件仅贴关键代码,其余请参考源码。库文件中利用StrokeableLabel进行排版以达到横/竖排和字符间距的效果:
private void CreateText(string newStr)
{
this.Children.Clear();
if (newStr == null)
return;
this.Orientation = TextOrientation;
for (int i = 0; i < newStr.Length; i++)
{
if (i < newStr.Length - 1)
addChar(newStr[i], false);
else
addChar(newStr[i], true);
}
}
/// <summary>
/// 添加一个字符
/// </summary>
/// <param name="c"></param>
private void addChar(char c, bool ignore)
{
StrokeableLabel label = new StrokeableLabel();
label.Text = c + "";
label.Fill = this.Fill;
label.Stroke = this.Stroke;
label.StrokeThickness = this.StrokeThickness;
label.FontSize = this.FontSize;
label.FontFamily = this.FontFamily;
label.FontStyle = this.FontStyle;
label.FontWeight = this.FontWeight;
label.FontStretch = this.FontStretch;
if (!ignore)
switch (Orientation)
{
case System.Windows.Controls.Orientation.Horizontal:
label.Margin = new Thickness(0, 0, StretchSize, 0);
break;
case System.Windows.Controls.Orientation.Vertical:
label.Margin = new Thickness(0, 0, 0, StretchSize);
break;
}
label.VerticalAlignment = System.Windows.VerticalAlignment.Center;
label.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
this.Children.Add(label);
}
源码:
http://download.csdn.net/detail/wblct/8708017