博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
阅读量:7095 次
发布时间:2019-06-28

本文共 2924 字,大约阅读时间需要 9 分钟。

原文:

自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能。另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好了。

由于本人比较懒,没有重新做,文字竖排和字符间距主要是通过新建继承自StackPanel的FormatedText类逐字符添加StrokeableLabel做的,竖排是用的StackPanel.Orientation来设置的,字符间距主要用的StrokeableLabel.Margin。

对于StrokeableLabel只是添加了设计时支持,其他没有改变,如果对StrokeableLabel不了解请看

FormatedText有如下新添属性:

StretchSize:字符间距

TextOrientation:文字排版

下面是效果图

XMAL配置:

库文件仅贴关键代码,其余请参考源码。库文件中利用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);            }        }        ///         /// 添加一个字符        ///         ///         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://aaaql.baihongyu.com/

你可能感兴趣的文章