原文:WPF 字体描边的实现方式

 

        <local:TextPath x:Name="PathEdge"
Fill="Red"
Stroke="Yellow"
StrokeThickness="2"
FontFamily="Ariel"
FontSize="72"
FontWeight="Bold"
Text="This is a Test of Outline Text"/>

 

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes; namespace WpfOutlineFontSample
{
/// <summary>
/// This class generates a Geometry from a block of text in a specific font, weight, etc.
/// and renders it to WPF as a shape.
/// </summary>
public class TextPath : Shape
{
private Geometry _textGeometry; #region Dependency Properties
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(typeof(TextPath),
new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[Localizability(LocalizationCategory.Font)]
[TypeConverter(typeof(FontFamilyConverter))]
public FontFamily FontFamily { get { return (FontFamily)GetValue(FontFamilyProperty); } set { SetValue(FontFamilyProperty, value); } } public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(TextPath),
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[TypeConverter(typeof(FontSizeConverter))]
[Localizability(LocalizationCategory.None)]
public double FontSize { get { return (double)GetValue(FontSizeProperty); } set { SetValue(FontSizeProperty, value); } } public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(typeof(TextPath),
new FrameworkPropertyMetadata(TextElement.FontStretchProperty.DefaultMetadata.DefaultValue,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[TypeConverter(typeof(FontStretchConverter))]
public FontStretch FontStretch { get { return (FontStretch)GetValue(FontStretchProperty); } set { SetValue(FontStretchProperty, value); } } public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(typeof(TextPath),
new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[TypeConverter(typeof(FontStyleConverter))]
public FontStyle FontStyle { get { return (FontStyle)GetValue(FontStyleProperty); } set { SetValue(FontStyleProperty, value); } } public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(typeof(TextPath),
new FrameworkPropertyMetadata(SystemFonts.MessageFontWeight,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.Inherits,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[TypeConverter(typeof(FontWeightConverter))]
public FontWeight FontWeight { get { return (FontWeight)GetValue(FontWeightProperty); } set { SetValue(FontWeightProperty, value); } } public static readonly DependencyProperty OriginPointProperty =
DependencyProperty.Register("Origin", typeof(Point), typeof(TextPath),
new FrameworkPropertyMetadata(new Point(0, 0),
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
[TypeConverter(typeof(PointConverter))]
public Point Origin { get { return (Point)GetValue(OriginPointProperty); } set { SetValue(OriginPointProperty, value); } } public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TextPath),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
OnPropertyChanged));
[Bindable(true), Category("Appearance")]
public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
#endregion protected override Geometry DefiningGeometry
{
get { return _textGeometry;} //_textGeometry = Geometry.Empty;//_textGeometry ;
} private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((TextPath)d).CreateTextGeometry();
} private void CreateTextGeometry()
{
var formattedText = new FormattedText(Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black);
_textGeometry = formattedText.BuildGeometry(Origin);
} protected override Size MeasureOverride(Size availableSize)
{
if (_textGeometry == null) CreateTextGeometry();
if (_textGeometry.Bounds == Rect.Empty)
return new Size(0, 0);
// return the desired size
return new Size(Math.Min(availableSize.Width, _textGeometry.Bounds.Width), Math.Min(availableSize.Height, _textGeometry.Bounds.Height));
}
}
}

 

WPF 字体描边的实现方式的更多相关文章

  1. cocos2d-x 全面总结--字体描边和制作阴影

    关于字体描边的实现,不考虑效果和效率的话,是有三种方式: ① 利用CCLabelTTF制作文字描边和阴影效果 ② 利用CCRenderTexture渲染文理的方式生成带有描边效果的文字 ③ 利用sha ...

  2. cocos2dx 字体描边方法介绍

    转载地址:http://www.taikr.com/group/2/thread/1606 关于cocos2dx 字体描边的实现,不考虑效果和效率的话,是有三种方式: ① 利用CCLabelTTF制作 ...

  3. cocosbuilder中使用字体描边时,字符重叠,间距过小问题

    cocosbuilder中使用字体描边时,字符重叠,间距过小问题 cocos2d-x 3.7 v3.7解析cocosbuilder中描边字体的代码如下: void LabelTTFLoader::pa ...

  4. Font Awesome矢量版,十六进制版,WPF字体使用

    我之前在博客中介绍过几个矢量图库网站,在WPF程序中,一般接触到的矢量图标资源有XAML.SVG.字体这三种格式.XAML是标准格式就不说了,SVG并不是直接支持的,不过微软提供了Expression ...

  5. WPF字体模糊解决方案

    原文:WPF字体模糊解决方案 WPF对字体渲染做了很大的改善,与Winform窗体字体相比较,更加平滑,但是当字体大小较小时,则会出现字体模糊的现象.可通过以下方法进行改善处理: 对于XAML用户界面 ...

  6. cocos2dx 字体描边遇到的描边缺失的bug

    在cocos中,设置字体描边可以用enableOutline(cc.c4b(30, 10, 0, 255), 2)函数设置,第一个参数是字体颜色,第二个参数是描边轮廓大小,单位是2个像素, 我在使用过 ...

  7. WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整

    原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...

  8. WPF 海康威视网络摄像头回调方式实现断连提示,降低时延

    原文:WPF 海康威视网络摄像头回调方式实现断连提示,降低时延 项目需要使用海康威视网络摄像头接入实时视频数据,使用海康威视官方SDK开发,发现没有断连提示的功能,故开发了一个断连提示的功能 在开发过 ...

  9. WPF字体图标——IconFont

    原文:WPF字体图标--IconFont 版权声明:本文为[CSDN博主:松一160]原创文章,未经允许不得转载. https://blog.csdn.net/songyi160/article/de ...

随机推荐

  1. 暴力模拟——cf988E

    很简单的题,就是模拟一下 #include<bits/stdc++.h> using namespace std; #define ll long long ll n,a[],len; i ...

  2. Python-爬虫-HTTP协议请求之GET请求

    我们在百度搜索时,输入关键词,比如“hello”,URL发生变化,如下: https://www.baidu.com/s?wd=hello&rsv_spt=1&rsv_iqid=0xf ...

  3. 由于某些原因无法博客搬家,现在换马甲了 http://blog.csdn.net/qq_32066409

    由于位置原因无法博客搬家,现在换马甲到csdn了地址如下http://blog.csdn.net/qq_32066409

  4. Docker的概念及基本用法

    Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源.Docker提供了一种在安全.可 ...

  5. JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值

    随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...

  6. Jquery中attr与prop的区别详解

    prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...

  7. jpa 踩坑 SQLGrammarException

    SQLGrammarException could not execute query  cause by not found column id ,, id指的是,返回的结果没有Id 封装结果集出错 ...

  8. truncate、delete、drop

    相同点: 1.三者共同点: truncate.不带where字句的delete.drop都会删除表内的数据 2.drop.truncate的共同点: drop.truncate都得DDL语句(数据库定 ...

  9. Algo: Majority Element

    Approach #1 Brute Force Intuition    We can exhaust the search space in quadratic time by checking w ...

  10. thinkphp 正则路由

    正则路由也就是采用正则表达式定义路由的一种方式,依靠强大的正则表达式,能够定义更灵活的路由规则. 路由表达式支持的正则定义必须以“/”开头,否则就视为规则表达式.也就是说如果采用: '#^blog\/ ...