原文: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







WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整的更多相关文章

  1. WPF文字描边的解决方法

    原文:WPF文字描边的解决方法  由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊.最后求助于MSDN,找到了方案,和大家分 ...

  2. echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转

    Echarts 标签中文本内容太长的时候怎么办 ? 关于这个问题搜索一下,有很多解决方案.无非就是 省略(间隔显示).旋转文字方向.竖排展示 前面两种解决方案,就是echarts暴露的: {   ax ...

  3. 使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行

    使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行 前几天项目中有一个图表的是用echart生成的,遇到一个问题,就是在手机端显示的售时候,如果文字太长就会超出div,之前的 ...

  4. FusionCharts使用问题及解决方法(二)-FusionCharts常见问题大全

    在上文中,我们介绍了FusionCharts常见问题(FAQ)的解决方法,本文将一同讨论FusionCharts使用者面临的一些复杂问题的解决方法. 如何启用JavaScript调试模式? 要启用Ja ...

  5. Unity导出APk出错解决方法二

    错误提示(需得打开编辑器log文件才能看到全部log,Unity3d只显示一部分): Error building Player: CommandInvokationFailure: Unable t ...

  6. RecyclerView嵌套TextView时显示文字不全的解决方法之一

    先描述一下这个小bug:简单的TextView嵌套RecyclerView作为itemView时,可能会在文本中出现布局覆盖的现象,itemView的布局其实很简单,就是一个RelativeLayou ...

  7. PHP中Strict Standards错误解决方法二

    在PHP5.3.3 中安装wordpress 3.0.1 ,在安装时出现错误:Strict Standards: PHP Strict Standards: Declaration of Walker ...

  8. Xcode真机测试could not find developer disk image解决方法(支持iOS9.2)

    这个问题开发者经常碰到,因为当我们更新手机iOS版本的时候,可能我们开发人员因为项目的需要等原因并一定愿意更新xcode到最新版本.但是老版本的xcode极有可能不支持最新的iOS版本,也有一些旧的i ...

  9. input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法

    1.只允许输入数字: <input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'') ...

随机推荐

  1. Ajax之旅(二)--XMLHttpRequest

         上文中提到的Ajax的异步更新.主要使用XMLHttpRequest对象来实现的,XMLHttpRequest对象能够在不向server提交整个页面的情况下,实现局部更新网页. 当页面所有载 ...

  2. 嵌入式arm linux环境中gdb+gdbserver调试

    一.前言嵌入式Linux系统中,应用开发过程中,很多情况下,用户需要对一个应用程序进行反复调试,特别是复杂的程序.采用GDB方法调试,由于嵌入式系统资源有限性,一般不能直接在目标系统上进行调试,通常采 ...

  3. codeforces 571B--Minimization(贪心+dp)

    D. Minimization time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. jquery 无刷新上传的小function

    function zll_up(click_id,up_url,text_id,show_id){ this.create = function(){} //当点击指定元素时,创建iframe for ...

  5. [HTML] Creating visual skip links in HTML and CSS

    Skip links are an extremely helpful navigation pattern for keyboard and screen reader users, since t ...

  6. [D3] Convert Input Data to Output Values with Linear Scales in D3

    Mapping abstract values to visual representations is what data visualization is all about, and that’ ...

  7. windows 空闲超时 非管理员如何破解

    windows 空闲超时 非管理员如何破解

  8. SpringMVC接受参数若干问题

    最近2年在工作问题总结中,好几次遇到了SpringMVC接收参数的问题,今天特别总结下.  SpringMVC接收参数的方法:  Html参数输入: <input name="stat ...

  9. bootstrap如何把表单select input button弄在一行

    bootstrap很多折叠样式css都已经写好,可以直接用,很方便.但是,如果遇到一些bootstrap文档里面没有的例子,估计很多初学者都懵了,然后会折腾很久也未见得有效.今天主要讲如何把selec ...

  10. 【AJAX】AJAX实现搜索信息自己主动推荐并补全

    好久没有继续看AJAX的视频教程了,今天就将最后一个教程案例做完.我们在搜索引擎中输入文字时文本框下会提示对应的信息,这个案例就是实现这样的基本功能,代码比較粗糙还须要进一步完好,当中有些地方也须要向 ...