- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在将字体从资源加载到 PrivateFontCollection 时遇到一些困难。
当我开始这个时,我成功地从文件中加载了字体,但是我希望将字体嵌入到我的项目中(这样用户端的文件困惑就会减少,应用程序运行时的 IO 也会少一些)运行)。
以下代码将加载字体、获取正确的名称并允许缩放,但没有一个字符正确显示。
static class Foo {
public static string FontAwesomeTTF { get; set; }
public static Font FontAwesome { get; set; }
public static float Size { get; set; }
public static FontStyle Style { get; set; }
private static PrivateFontCollection pfc { get; set; }
static Foo() {
// This was set when loading from a file.
// FontAwesomeTTF = "fontawesome-webfont.ttf";
Style = FontStyle.Regular;
Size = 20;
if ( pfc==null ) {
pfc=new PrivateFontCollection();
if ( FontAwesomeTTF==null ) {
var fontBytes=Properties.Resources.fontawesome_webfont;
var fontData=Marshal.AllocCoTaskMem( fontBytes.Length );
Marshal.Copy( fontBytes, 0, fontData, fontBytes.Length );
pfc.AddMemoryFont( fontData, fontBytes.Length );
Marshal.FreeCoTaskMem( fontData );
} else {
pfc.AddFontFile( FontAwesomeTTF );
}
}
FontAwesome = new Font(pfc.Families[0], Size, Style);
}
private static string UnicodeToChar( string hex ) {
int code=int.Parse( hex, System.Globalization.NumberStyles.HexNumber );
string unicodeString=char.ConvertFromUtf32( code );
return unicodeString;
}
public static string glass { get { return UnicodeToChar("f000"); } }
}
示例用法:
label1.Font = Foo.FontAwesome;
label1.Text = Foo.glass;
我在嵌入式资源和基于文件的测试中都使用了当前的 FontAwesome TTF 文件。似乎当嵌入的东西在翻译中或从嵌入加载时丢失或加扰。我需要帮助才能完成这项工作,以便我可以将字体从嵌入式资源加载到 PrivateFontCollection 中。
我在 SO 上查看了一些“解决方案”,但它们已经过时,并且 Visual Studio 2013 中不再提供某些或所有命令/访问器(文章解决方案来自 4-5 年前)。一些示例“解决方案”以及它们为何不起作用:
Solution #1 - 这不起作用,因为字体的访问器字符串返回 null。在我的例子中,访问器是 MyProject.Properties.Resources.fontawesome_webfont
Solution #2 - 此解决方案最接近,但再次用于访问资源的方法不再有效。我上面的代码实现了一个收获版本,去掉了将 byte[] 数组传递到内存然后从内存加载的核心概念。由于在我的例子中资源的 get{} 属性已经返回一个 byte[] 数组,所以没有必要将它“转换”为字节数组,因此我(看似)能够安全地删除那部分代码更新它以使用更新的访问器。
无论哪种情况,我都想要一个解决这个问题的方法,允许我将字体文件从嵌入式资源加载到 PrivateFontCollection 中。
最佳答案
AddMemoryFont
's remarks section说:
To use the memory font, text on a control must be rendered with GDI+. Use the
SetCompatibleTextRenderingDefault
method, passingtrue
, to set GDI+ rendering on the application, or on individual controls by setting the control'sUseCompatibleTextRendering
property totrue
. Some controls cannot be rendered with GDI+.
我在本地重现了您的问题,并将您的使用示例更改为:
label1.Font = Foo.FontAwesome;
label1.Text = Foo.glass;
label1.UseCompatibleTextRendering = true;
这会为该标签打开 GDI+ 呈现,允许使用 AddMemoryFont
添加的字体正确呈现。
关于c# - 将资源中的字体加载到 PrivateFontCollection 会导致损坏重新发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33087848/
有人告诉我,如果我只有一个“东西”,比如家(不是多个家),我应该在 routes.rb 中使用资源 :home,而不是资源 :home。但是当我查看路由时,POST 函数似乎想要 home#creat
Activity 开始。这些代码框架顺利通过。 // Initialize array adapters. One for already paired devices and //
资源 search-hadoop.com search-hadoop.com索引所有邮件列表,非常适合历史搜索。当你遇到问题时首先在这里搜索,因为很可能有人已经遇到了你的问题。 邮件列表 在A
我是 WPF 的新手,正在努力使用位于单独程序集中的样式。这就是我正在做的:- 我有一个带有\Themes 文件夹的类库项目,其中包含一个“generic.xaml”,它合并了\Themes 内的子文
我正在编写一个使用虚拟树状文件结构的插件。基本上它就像一个包含文件的标准文件系统,区别在于这些文件实际上并不存在于文件系统中的特定位置,而只是 java 对象。 这些当前由使用 SettingProv
如果我在 XAML 中使用以下内容,我会收到错误消息: 错
我正在使用 laravel 资源来获取 api 的数据: return [ 'id' => $this->id, 'unread' =>
我有以下 pom.xml: 4.0.0 mycompany resource-fail 0.0.1-SNAPSHOT BazBat
许多GDI +类都实现IDisposable,但是我不确定何时应该调用Dispose。对于使用new或静态方法(例如Graphics.CreateGraphics)创建的实例来说,这很明显。但是,由属
我正在构建一组 RESTful 资源,其工作方式如下:(我将使用“people”作为示例): 获取/people/{key} - 返回一个人对象 (JSON) GET/people?first_nam
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一个使用 $resource 的简单 Controller : var Regions = $resource('mocks/regions.json'); $scope.regions =
在 Azure 门户中,如何查看不同资源之间的依赖关系。我特别想查看哪些资源正在使用我要删除的存储。 最佳答案 您可以使用应用程序洞察应用程序 map 来执行此操作: 您还可以打开存储帐户的日志记录:
我正在使用 ionic 生成资源(图标和启动画面)。我正在使用 ionic v2.1.0 和 cordova v6.4.0。 到目前为止我一直在使用(它在以前的版本中工作): cordova plat
是否可以使用 Assets 包含子文件夹中的文件? 示例:[base_url]/assets/css/pepper-grinder/jquery-ui-1.8.11.custom.min.css 最佳
我正在阅读一些尝试教授 Android 开发的书。在书中,作者概述了 res/下的一些目录。他提到 res/menu 包含基于 XML 的菜单规范。他还提到了保存“通用文件”的 res/raw。当我创
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在服务器上使用 express-resource。在我的 AngularJS Controller 中: var User = $resource('/services/users/:use
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。 现在设置我使用的授权 header : NSString *result = [NSString st
我是一名优秀的程序员,十分优秀!