c# - 如何使用 xamarin 将私有(private)字符放入字符串中

标签 c# fonts xamarin xamarin.ios xamarin.android

我想在我的 Xamarin 项目中使用 metro 字体 ( http://metroui.org.ua/font.html ) 中的一些图标,我找不到在我的字符串上放置一个私有(private)使用的字符的解决方案,它总是被一个带有 ? 里面。

如果有人知道我如何放置特殊字符,那对我真的很有帮助!

最佳答案

在 Android 中,您需要一个自定义渲染器。其余的应该开箱即用。 This example使用 FontAwesome,但也应该适用于您的。

首先让我们确保以正确的方式包含您的字体。

如果是 Android,您需要将字体放入您的 Assets 文件夹并将其标记为 BundleAsset

在 iOS 的情况下,将其复制到 Resources 文件夹并将其标记为 BundleResource 并将其设置为“始终复制”。最后编辑 info.plist 并添加

 <key>UIAppFonts</key>
 <array>
   <string>FontAwesome.ttf</string>
</array>

然后 Android 的自定义渲染器将如下所示:

[assembly: ExportRenderer(typeof(FontAwesomeIcon), typeof(FontAwesomeIconRenderer))]

namespace AAA.Android.Renderers
{
    /// <summary>
    /// Add the FontAwesome.ttf to the Assets folder and mark as "Android Asset"
    /// </summary>
   public  class FontAwesomeIconRenderer: LabelRenderer
    {
       protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
       {
           base.OnElementChanged(e);
           if (e.OldElement == null)
           {
               //The ttf in /Assets is CaseSensitive, so name it FontAwesome.ttf
               Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, FontAwesomeIcon.Typeface + ".ttf");
           }
       }
    }
}

这是它的自定义 Label 实现:

namespace AAA.Common.Views.Shared.FontAwesome
{
    public class FontAwesomeIcon : Label
    {
        //Must match the exact "Name" of the font which you can get by double clicking the TTF in Windows
        public const string Typeface = "FontAwesome";  

        public FontAwesomeIcon(string fontAwesomeIcon=null)
        {
            FontFamily = Typeface;    //iOS is happy with this, Android needs a renderer to add ".ttf"
            Text = fontAwesomeIcon;
        }

        /// <summary>
        /// Get more icons from http://fortawesome.github.io/Font-Awesome/cheatsheet/
        /// Tip: Just copy and past the icon picture here to get the icon
        /// </summary>
        public static class Icon
        {
            public static readonly string Gear = "";
            public static readonly string Globe = "";
        }
    }
}

在静态类 Icon 中,您可以添加要使用的图标。 还有 here你可以找到一个 ascii 代码列表,而不是奇怪的问号图标。

关于c# - 如何使用 xamarin 将私有(private)字符放入字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484980/

相关文章:

c# - 为每组端点使用静态 HttpClient 与不同的 HTTPClient

visual-studio - Mac 上的 IPA 在哪里(当它用作构建服务器时)?

c# - 文档设计 - 更新评论

c# - "Transparent"Windows 窗体在 DirectDraw Video Surface 前面时闪烁

c# - 使用 Entity Framework 读取 SQL Server 表时对象名称无效

java - 从Java中的字体文件中检索字体名称

emacs - 如何在emacs中设置自定义字体?

java - 需要一种缩放字体以适合矩形的方法

android - 无法为学生创建 Xamarin 帐户

c# - SQL 命令不打印到标签