<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>简单生活 -- Kevin Yang的博客 &#187; 中文字体</title>
	<atom:link href="http://www.imkevinyang.com/tags/%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93/feed" rel="self" type="application/rss+xml" />
	<link>http://www.imkevinyang.com</link>
	<description>It&#039;s all about sharing</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:37:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>解决Silverlight引用中文字体的问题</title>
		<link>http://www.imkevinyang.com/2009/09/%e8%a7%a3%e5%86%b3silverlight%e5%bc%95%e7%94%a8%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93%e7%9a%84%e9%97%ae%e9%a2%98.html</link>
		<comments>http://www.imkevinyang.com/2009/09/%e8%a7%a3%e5%86%b3silverlight%e5%bc%95%e7%94%a8%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93%e7%9a%84%e9%97%ae%e9%a2%98.html#comments</comments>
		<pubDate>Mon, 07 Sep 2009 16:34:19 +0000</pubDate>
		<dc:creator>Kevin Yang</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[中文字体]]></category>
		<category><![CDATA[字体]]></category>
		<category><![CDATA[字体打包]]></category>
		<category><![CDATA[引用]]></category>
		<category><![CDATA[本地字体]]></category>

		<guid isPermaLink="false">http://www.imkevinyang.com/2009/09/%e8%a7%a3%e5%86%b3silverlight%e5%bc%95%e7%94%a8%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93%e7%9a%84%e9%97%ae%e9%a2%98.html</guid>
		<description><![CDATA[
问题描述
<p>在Silverlight程序中，对文字设置了中文字体之后不起任何作用。</p>
问题分析
<p>Silverlight内置了十几种常见拉丁字体，如Arial，Comic Sans MS，Courier New，Lucida Sans Unicode等。默认使用的字体是Portable User Interface。这是一种&#8230;</p>]]></description>
			<content:encoded><![CDATA[</p>
<h2>问题描述</h2>
<p>在Silverlight程序中，对文字设置了中文字体之后不起任何作用。</p>
<h2>问题分析</h2>
<p>Silverlight内置了十几种常见拉丁字体，如Arial，Comic Sans MS，Courier New，Lucida Sans Unicode等。默认使用的字体是Portable User Interface。这是一种复合字体，Silverlight会根据文字的语言选择最佳的字体。但是，这种字体通常在显示中文的时候非常难看。下图是默认的显示效果：</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.imkevinyang.com/wp-content/uploads/2009/09/image_thumb6.png" width="404" height="51" /> </p>
<p>所以，我们需要在程序中手动指定文字的字体。</p>
<h3>三种字体引用方式</h3>
<p>如果使用的字体是Silverlight内置支持的，那么无需做任何配置，直接指定FontFamily即可。内置的字体有Arial，Arial Black，Comic Sans MS，Courier New，Lucida Sans Unicode，Times New Roman，Georgia，Trebuchet MS，Verdana和Webdings。使用这些字体不依赖于客户端操作系统。</p>
<p>同时Silverlight也支持引用本地系统一部分字体（注意，不是全部本地字体都支持）。如Segoe UI，Symbol，Tahoma，宋体，等等。使用这些字体需要注意，其他客户端系统中可能不存在这些字体，如英文操作系统一般没有宋体。那么如果你使用了宋体那就会有问题。</p>
<p>如果你需要让Silverlight应用程序在不同系统下都有一致的字体，或者你想用某些字体，但是这些字体不支持本地引用（如隶书），那么Silverlight允许你将字体文件打包到应用程序中，可以放在Xap包中，也可以放在ClientBin目录下，然后在代码中动态下载并引用这些字体文件。这样牺牲的代价就是Xap包会变得较大。</p>
<p>为了更好显示中文，我需要为上面的文字显式指定使用“宋体”。代码如下：</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Grid</span> <span class="attr">x:Name</span><span class="kwrd">=&quot;LayoutRoot&quot;</span> <span class="attr">Background</span><span class="kwrd">=&quot;White&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Text</span><span class="kwrd">=&quot;Hello，测试中英文混合显示效果&quot;</span> <span class="attr">FontSize</span><span class="kwrd">=&quot;26&quot;</span> <span class="attr">FontFamily</span><span class="kwrd">=&quot;宋体&quot;</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Grid</span><span class="kwrd">&gt;</span></pre>
<p>Ctrl+F5运行，却发现字体没有发生明显的变化。说明字体的设置不起作用。我又测试了一下将宋体打包到应用程序中。为了简单，我使用Blend3帮我自动完成这个操作。</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Blend 3中字体打包" border="0" alt="Blend 3中字体打包" src="http://www.imkevinyang.com/wp-content/uploads/2009/09/image_thumb7.png" width="246" height="140" /> </p>
<p>选中一种字体之后，勾选字体面板下面的Embed复选框即可将指定字体打包到Silverlight工程中去。这个时候工程中自动生成了Fonts文件夹，里面放置了宋体这个字体文件。</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Visual Studio 打包字体" border="0" alt="Visual Studio 打包字体" src="http://www.imkevinyang.com/wp-content/uploads/2009/09/image_thumb8.png" width="165" height="112" /> </p>
<p>同时，Xaml也发生了变化，由引用系统的宋体改成引用Fonts.zip包中的宋体了。如下：</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Grid</span> <span class="attr">x:Name</span><span class="kwrd">=&quot;LayoutRoot&quot;</span> <span class="attr">Background</span><span class="kwrd">=&quot;White&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Text</span><span class="kwrd">=&quot;Hello，测试中英文混合显示效果&quot;</span> <span class="attr">FontSize</span><span class="kwrd">=&quot;26&quot;</span> <span class="attr">FontFamily</span><span class="kwrd">=&quot;Fonts/Fonts.zip#宋体&quot;</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Grid</span><span class="kwrd">&gt;</span></pre>
<p>运行结果发现字体依旧。</p>
<p>后来查阅Silverlight官方文档的时候，看到字体列表一节时，突然想到，<strong><font color="#008000">是不是因为字体名不规范？不能直接使用“宋体”，而应该使用Simsun</font></strong>。赶紧测试了一下，发现果然是这个问题。只需将上面两个示例代码中的宋体改成Simsun即可正确显示出宋体的效果来。</p>
<p><img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.imkevinyang.com/wp-content/uploads/2009/09/image_thumb9.png" width="388" height="33" /> </p>
<p>后来又测试了一下其它中文字体，如微软雅黑，如果直接这样写中文的话是显示不出来的，一定要写“Microsoft YaHei”。可恶的Blend，帮我自动生成的代码居然有这种莫名其妙的错。不知道这算是Silverlight和Blend的Bug还是设计使然。</p>
<h2>总结</h2>
<p>在Silverlight中，如果要引用字体，一定不能直接写上中文，否则不起任何作用。如果你是在英文操作系统下的话，你可以打开字体文件（C:\windows\fonts文件夹下，Silverlight支持ttf,odttf和otf这几种后缀的字体文件），其中的字体名称一栏就是FontFamily所使用的名称。只是在中文操作系统下，支持中文的字体在这里会显示为中文名，而不是英文名。</p>
<p><img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="FontFamily" border="0" alt="FontFamily" src="http://www.imkevinyang.com/wp-content/uploads/2009/09/image_thumb10.png" width="316" height="132" /> </p>
<p>常见中文字体名和英文字体名的对应关系如下：</p>
<table border="1" cellspacing="0" cellpadding="0" height="400">
<thead>
<tr>
<td colspan="2" align="center">常见中文字体与FontFamily映射表 </td>
</tr>
</thead>
<tbody>
<tr>
<td valign="top" width="171">宋体（英文不等宽） </td>
<td valign="top" width="429">Simsun </td>
</tr>
<tr>
<td valign="top" width="171">宋体（英文等宽） </td>
<td valign="top" width="429">NSimsun </td>
</tr>
<tr>
<td valign="top" width="171">楷体 </td>
<td valign="top" width="429">KaiTi </td>
</tr>
<tr>
<td valign="top" width="171">黑体 </td>
<td valign="top" width="429">SimHei </td>
</tr>
<tr>
<td valign="top" width="171">仿宋 </td>
<td valign="top" width="429">FangSong </td>
</tr>
<tr>
<td valign="top" width="171">微软正黑体 </td>
<td valign="top" width="429">Microsoft JhengHei </td>
</tr>
<tr>
<td valign="top" width="171">细明体 </td>
<td valign="top" width="429">MingLiu </td>
</tr>
<tr>
<td valign="top" width="171">微软雅黑 </td>
<td valign="top" width="429">Microsoft YaHei </td>
</tr>
<tr>
<td valign="top" width="171">隶书 </td>
<td valign="top" width="429">LiSu（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">华文彩云 </td>
<td valign="top" width="429">STCaiyun（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">华文琥珀 </td>
<td valign="top" width="429">STHupo（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">华文隶书 </td>
<td valign="top" width="429">STLiti（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">华文新魏 </td>
<td valign="top" width="429">STXinwei（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">华文行楷 </td>
<td valign="top" width="429">STXingkai（只能打包，不支持本地系统引用） </td>
</tr>
<tr>
<td valign="top" width="171">幼圆 </td>
<td valign="top" width="429">YouYuan（只能打包，不支持本地系统引用） </td>
</tr>
</tbody>
</table>
<p align="right">——<a href="http://www.imkevinyang.com/"><em><strong>Kevin Yang</strong></em></a></p>

	标签：<a href="http://www.imkevinyang.com/categories/techarticles/silverlight" title="Silverlight" rel="tag">Silverlight</a>, <a href="http://www.imkevinyang.com/tags/silverlight" title="Silverlight" rel="tag">Silverlight</a>, <a href="http://www.imkevinyang.com/tags/%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93" title="中文字体" rel="tag">中文字体</a>, <a href="http://www.imkevinyang.com/tags/%e5%ad%97%e4%bd%93" title="字体" rel="tag">字体</a>, <a href="http://www.imkevinyang.com/tags/%e5%ad%97%e4%bd%93%e6%89%93%e5%8c%85" title="字体打包" rel="tag">字体打包</a>, <a href="http://www.imkevinyang.com/tags/%e5%bc%95%e7%94%a8" title="引用" rel="tag">引用</a>, <a href="http://www.imkevinyang.com/tags/%e6%9c%ac%e5%9c%b0%e5%ad%97%e4%bd%93" title="本地字体" rel="tag">本地字体</a><br />

	<h4 style="background-color:#3B3B3B;border-bottom:2px groove gray;color:#F2F2F2;margin-top:20px;padding:6px 6px 6px 15px;margin:20px 0px 0px 0px">你可能对下面的文章感兴趣</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.imkevinyang.com/2009/09/blend%e5%b0%8f%e8%b4%b4%e5%a3%ab%e6%94%b9%e5%8f%98%e9%bc%a0%e6%a0%87%e6%bb%9a%e8%bd%ae%e7%9a%84%e9%bb%98%e8%ae%a4%e8%a1%8c%e4%b8%ba-2.html" title="Blend小贴士&mdash;&mdash;改变鼠标滚轮的默认行为 (2009/09/18)">Blend小贴士&mdash;&mdash;改变鼠标滚轮的默认行为</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/02/silverlight-2-rtm-%e5%a4%9a%e5%9b%bd%e8%af%ad%e8%a8%80%e6%94%af%e6%8c%81%ef%bc%88%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e6%9c%ac%e5%9c%b0%e5%8c%96%ef%bc%89.html" title="Silverlight 2 RTM 多国语言支持（应用程序本地化） (2009/02/28)">Silverlight 2 RTM 多国语言支持（应用程序本地化）</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/03/silverlight-2%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bdxap%e5%8c%85%e4%b8%ad%e7%9a%84%e7%a8%8b%e5%ba%8f%e9%9b%86.html" title="Silverlight 2动态加载Xap包中的程序集 (2009/03/02)">Silverlight 2动态加载Xap包中的程序集</a> </li>
	<li><a href="http://www.imkevinyang.com/2010/05/silverlight-4ria-services-%e6%90%9c%e7%b4%a2%e5%bc%95%e6%93%8e%e4%bc%98%e5%8c%96seo.html" title="Silverlight 4+RIA Services&#8211;搜索引擎优化(SEO) (2010/05/04)">Silverlight 4+RIA Services&#8211;搜索引擎优化(SEO)</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/04/silverlight-seo%ef%bc%88%e6%90%9c%e7%b4%a2%e5%bc%95%e6%93%8e%e4%bc%98%e5%8c%96%ef%bc%89%e7%99%bd%e7%9a%ae%e4%b9%a6.html" title="Silverlight SEO（搜索引擎优化）白皮书 (2009/04/06)">Silverlight SEO（搜索引擎优化）白皮书</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/03/silverlight2-%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bd%e7%a8%8b%e5%ba%8f%e9%9b%86-ondemand%e6%a8%a1%e5%bc%8fdynamic-load-assembly.html" title="Silverlight2 动态加载程序集&#8212;OnDemand模式(Dynamic Load Assembly) (2009/03/02)">Silverlight2 动态加载程序集&#8212;OnDemand模式(Dynamic Load Assembly)</a> </li>
	<li><a href="http://www.imkevinyang.com/2011/09/silverlight%e4%b8%ad%e6%97%a0%e6%b3%95%e8%ae%be%e7%bd%ae%e5%8d%ab%e6%98%9f%e7%a8%8b%e5%ba%8f%e9%9b%86%e4%b8%ba%e4%b8%ad%e7%ab%8b%e8%b5%84%e6%ba%90%e7%a8%8b%e5%ba%8f%e9%9b%86.html" title="Silverlight中无法设置卫星程序集为中立资源程序集 (2011/09/05)">Silverlight中无法设置卫星程序集为中立资源程序集</a> </li>
	<li><a href="http://www.imkevinyang.com/2010/02/silverlight%e5%90%af%e7%94%a8assembly-caching%e4%b9%8b%e5%90%8e%e9%93%81%e9%80%9a%e7%94%a8%e6%88%b7%e6%97%a0%e6%b3%95%e8%ae%bf%e9%97%ae.html" title="Silverlight启用Assembly Caching之后铁通用户无法访问 (2010/02/13)">Silverlight启用Assembly Caching之后铁通用户无法访问</a> </li>
	<li><a href="http://www.imkevinyang.com/2010/03/silverlight%e7%9a%84%e4%be%9d%e8%b5%96%e5%b1%9e%e6%80%a7%e4%b8%8e%e9%99%84%e5%8a%a0%e5%b1%9e%e6%80%a7.html" title="Silverlight的依赖属性与附加属性 (2010/03/28)">Silverlight的依赖属性与附加属性</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/03/%e5%9b%be%e7%89%87%e7%ad%89%e8%b5%84%e6%ba%90%e7%9a%84%e5%bc%95%e7%94%a8%e8%b7%af%e5%be%84%e9%97%ae%e9%a2%98.html" title="图片等资源的引用路径问题 (2009/03/19)">图片等资源的引用路径问题</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.imkevinyang.com/2009/09/%e8%a7%a3%e5%86%b3silverlight%e5%bc%95%e7%94%a8%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93%e7%9a%84%e9%97%ae%e9%a2%98.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.imkevinyang.com @ 2012-02-09 10:26:43 -->
