在实际的APP中,带有图标的按钮用到地方还是蛮多的,字体图标往往能更快更生动的传达信息,并且相对于背景图片,字体图标也有着绝对的优势,所以实现按钮的字体图标是值得尝试的.

实现方法:各平台自定义渲染按钮

PCL

添加名为Fonts.cs的类,作用各平台的字体文件(ios-android-uwp,ios字体文件不要后缀并且大写,安卓全称)

  public static class Fonts
{
public static string IconFont= Device.OnPlatform("IconFont", "iconfont.ttf", null);
}

添加名为IconFonts.cs的类,定义所需要用到的字体,上述的字体文件可以去阿里妈妈字体库添加下载,然后打开.css文件,就可以看到字体编号"\eXXX",在这里加上u即可,

    public static class IconFonts
{
public static readonly string yuyin = "\ue667";
public static readonly string biaoqing = "\ue611";
public static readonly string gengduo = "\ue602";
public static readonly string xiangce = "\ue64e";
public static readonly string paizhao = "\ue6e5";
public static readonly string weizhi = "\ue63e";
public static readonly string fanhui = "\ue607";
public static readonly string dianhua = "\ue6dd";
public static readonly string yuyin1 = "\ue605";
public static readonly string yuyin2 = "\ue69f";
public static readonly string jianpan = "\ue63f";
public static readonly string fasong = "\ue60a";
public static readonly string shanchu = "\ue627";
}

Android

1添加一个名为ButtonTypefaceRenderer.cs的类,自定义渲染按钮(如果要扩展,在这里可以直接渲染需要扩展的元素即可,例如渲染Label)

[assembly: ExportRenderer(typeof(Label), typeof(LabelTypefaceRenderer))]
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(ButtonTypefaceRenderer))]
namespace Sample.Droid
{
class FontUtils
{
public static void ApplyTypeface(TextView view, string fontFamily)
{
if (!string.IsNullOrEmpty(fontFamily))
{
Typeface typeFace = null;
try
{
typeFace = Typeface.CreateFromAsset(Xamarin.Forms.Forms.Context.ApplicationContext.Assets, fontFamily);
}
catch (Exception ex)
{
Debug.WriteLine($"Could not load font {fontFamily}: {ex}");
} if (typeFace != null)
{
view.Typeface = typeFace;
}
}
}
}
//Label
public class LabelTypefaceRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e); FontUtils.ApplyTypeface(Control, Element.FontFamily);
}
} public class ButtonTypefaceRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e); FontUtils.ApplyTypeface(Control, Element.FontFamily);
} protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
FontUtils.ApplyTypeface(Control, Element.FontFamily);
}
}
}

2在Assets文件夹添加字体文件iconfont.ttf

Ios

1在资源文件夹添加字体文件iconfont.ttf

2在清单文件Info.plist,添加

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

用法

1引入Fonts.cs和FontIcons.cs的命名空间

2因为是渲染的所有按钮,所以不需要再在PCL上定义控件

            <Button x:Name="PhotoAlbum" FontSize="36"
Text="{x:Static styling:IconFonts.xiangce}"
FontFamily="{x:Static styling:Fonts.IconFont}"/>
<Button x:Name="TakePhoto" FontSize="36"
Text="{x:Static styling:IconFonts.paizhao}"
FontFamily="{x:Static styling:Fonts.IconFont}"/>
<Button x:Name="Lacation" FontSize="36"
Text="{x:Static styling:IconFonts.weizhi}"
FontFamily="{x:Static styling:Fonts.IconFont}"/>
<Button x:Name="ReturnHide" FontSize="36"
Text="{x:Static styling:IconFonts.fanhui}"
FontFamily="{x:Static styling:Fonts.IconFont}"/>

 

项目地址: https://github.com/weiweu/TestProject/tree/dev/ButtonFont

  

 

菜鸟的Xamarin.Forms前行之路——实现按钮的字体图标(可扩展)的更多相关文章

  1. 菜鸟的Xamarin.Forms前行之路——绪言

    作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过 ...

  2. 菜鸟的Xamarin.Forms前行之路——按钮的按下抬起事件的监控(可扩展至其他事件)

    提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件? 解决方法:各个平台自定义渲染依赖注入. 共享项目PCL: 1 ...

  3. 菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法

    项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/we ...

  4. 菜鸟的Xamarin.Forms前行之路——windows下VS运行ios模拟器调试

    在Xamarin.Forms项目中,运行安卓模拟器是很方便的,但是想要运行IOS模拟器,相对而言是困难一点. 在参考一些资料后,发现很多是与Xamarin.studio有关的方法,尝试了许久没有成功. ...

  5. 菜鸟的Xamarin.Forms前行之路——从新建项目到APP上架各种报错问题解决方法合集(不定时更新)

    出自:博客园-半路独行 原文地址:http://www.cnblogs.com/banluduxing/p/7425791.html 本文出自于http://www.cnblogs.com/banlu ...

  6. 菜鸟的Xamarin.Forms前行之路——共享组件

    出自:博客园-半路独行 本文出自于http://www.cnblogs.com/banluduxing 转载请注明出处. Url Description Xamarin.Social The Xama ...

  7. Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面

    今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...

  8. Xamarin.Forms 开发资源集合(复制)

    复制:https://www.cnblogs.com/mschen/p/10199997.html 收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 S ...

  9. Xamarin.Forms 开发资源集合

    收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 Snppts: Xamarin Forms UI Snippets. Prebuilt Templat ...

随机推荐

  1. 「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-16/ wx:key的高级特性.这个很重要,因为在app上经常有上拉,下拉加载,我们如果不使用这个特 ...

  2. kaptcha验证码组件使用简介

    Kaptcha是一个基于SimpleCaptcha的验证码开源项目. 官网地址:http://code.google.com/p/kaptcha/ kaptcha的使用比较方便,只需添加jar包依赖之 ...

  3. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  4. 使用NPM在项目中引入【lodash】

    mkdir [文件名 ] 创建项目文件 mkdir lodashDemo cd [文件名] 进入项目文件 cd lodashDemo nvm -v 查看nvm版本,确定nvm已安装 nvm -v No ...

  5. My97DatePicker日期控件的使用

    本文演示如何在MyEclipse项目中使用My97DatePicker日期控件 1.下载My97DatePicker日期控件, My97DatePicker日期控件下载地址 2.在MyEclipse项 ...

  6. C6 P5.2

    引用自 http://snippetinfo.net/media/117 下载源:php-5.2-x64.zip wget 源包.zip yum -y install httpd libXpm.so. ...

  7. Kafka源码分析

    本文主要针对于Kafka的源码进行分析,版本为kafka-0.8.2.1. 由于时间有限,可能更新比较慢... Kafka.scala // 读取配置文件 val props = Utils.load ...

  8. len=in.read(b,0,len)和len=in.read(b)的区别

    byte[] byte = new byte[1024]; int len =0 ; while((len=in.read(b))!=-1){ out.write(b,0,len); } read函数 ...

  9. Html5新瓶装老酒之一--Touch事件处理

    移动端的应用越来越多的开始采用html5来实现的.Html5有许多新特性需要开发者注意,比如css3,touch事件等等.比如做一个轮播图,分析其实现要领,有三点: 1.图片的轮播效果对应的css 样 ...

  10. 微信小程序(一)基本知识初识别

    最近微信圈里小程序很火的样子,以前小程序刚开始的时候研究了一下,多日没关注发现一些东西都淡忘了,最后决定还是记录下来的好.    毕竟好记星比不上烂笔头嘛~