WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
原文: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文字描边的解决方法(二)——支持文字竖排和字符间距调整的更多相关文章
- WPF文字描边的解决方法
原文:WPF文字描边的解决方法 由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊.最后求助于MSDN,找到了方案,和大家分 ...
- echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转
Echarts 标签中文本内容太长的时候怎么办 ? 关于这个问题搜索一下,有很多解决方案.无非就是 省略(间隔显示).旋转文字方向.竖排展示 前面两种解决方案,就是echarts暴露的: { ax ...
- 使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行
使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行 前几天项目中有一个图表的是用echart生成的,遇到一个问题,就是在手机端显示的售时候,如果文字太长就会超出div,之前的 ...
- FusionCharts使用问题及解决方法(二)-FusionCharts常见问题大全
在上文中,我们介绍了FusionCharts常见问题(FAQ)的解决方法,本文将一同讨论FusionCharts使用者面临的一些复杂问题的解决方法. 如何启用JavaScript调试模式? 要启用Ja ...
- Unity导出APk出错解决方法二
错误提示(需得打开编辑器log文件才能看到全部log,Unity3d只显示一部分): Error building Player: CommandInvokationFailure: Unable t ...
- RecyclerView嵌套TextView时显示文字不全的解决方法之一
先描述一下这个小bug:简单的TextView嵌套RecyclerView作为itemView时,可能会在文本中出现布局覆盖的现象,itemView的布局其实很简单,就是一个RelativeLayou ...
- PHP中Strict Standards错误解决方法二
在PHP5.3.3 中安装wordpress 3.0.1 ,在安装时出现错误:Strict Standards: PHP Strict Standards: Declaration of Walker ...
- Xcode真机测试could not find developer disk image解决方法(支持iOS9.2)
这个问题开发者经常碰到,因为当我们更新手机iOS版本的时候,可能我们开发人员因为项目的需要等原因并一定愿意更新xcode到最新版本.但是老版本的xcode极有可能不支持最新的iOS版本,也有一些旧的i ...
- input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法
1.只允许输入数字: <input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'') ...
随机推荐
- Oracle性能分析12:对象统计信息
对象统计信息描写叙述数据是如何在数据库中存储的,查询优化器使用这些统计信息来做出正确的决定.Oracle中有三种类型的对象统计信息:表统计.列统计和索引统计.而在每种类型中,有细分为:表或索引级别的统 ...
- 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏
转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...
- 漂亮的Android加载中动画:AVLoadingIndicatorView
AVLoadingIndicatorView 包含一组漂亮的Android加载中动画. IOS版本:here. 示例 Download Apk 用法 步骤1 Add dependencies in b ...
- php中模拟多继承如何实现
php中模拟多继承如何实现 一.总结 一句话总结:其实你继承别人也是想调用别人类里面的方法和属性,所以可以这样做:这本类中创建目标类的对象,然后通过这个对象来调用方法和属性,这样比继承来的方便. 二. ...
- GAN(Generative Adversarial Networks) 初步
1. Generator vs. Discriminator 首先需要指出的是生成式模型(generative models)和判别式模型(discriminative models)的区别: dis ...
- [D3] Load and Inspect Data with D3 v4
You probably use a framework or standalone library to load data into your apps, but what if that’s o ...
- Java虚拟机解析篇之---内存模型
今天闲来无事来,看一下Java中的内存模型和垃圾回收机制的原理.关于这个方面的知识,网上已经有非常多现成的资料能够供我们參考,可是知识还是比較杂的,在这部分知识点中有一本书不得不推荐:<深入理解 ...
- js进阶js中支持正则的四个常用字符串函数(search march replace split)
js进阶js中支持正则的四个常用字符串函数(search march replace split) 一.总结 代码中详细四个函数的用法 search march replace split 二.js进 ...
- USB 3.0规范中译本第9章 设备框架
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 设备框架可以被分成三层: 最底层是总线接口层,传送和接收包. 中间层处理在总线接口和设备的各种端点之间路由数 ...
- Spring之i18n配置与使用
Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...