菜鸟的Xamarin.Forms前行之路——实现按钮的字体图标(可扩展)
在实际的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前行之路——实现按钮的字体图标(可扩展)的更多相关文章
- 菜鸟的Xamarin.Forms前行之路——绪言
作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过 ...
- 菜鸟的Xamarin.Forms前行之路——按钮的按下抬起事件的监控(可扩展至其他事件)
提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件? 解决方法:各个平台自定义渲染依赖注入. 共享项目PCL: 1 ...
- 菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法
项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/we ...
- 菜鸟的Xamarin.Forms前行之路——windows下VS运行ios模拟器调试
在Xamarin.Forms项目中,运行安卓模拟器是很方便的,但是想要运行IOS模拟器,相对而言是困难一点. 在参考一些资料后,发现很多是与Xamarin.studio有关的方法,尝试了许久没有成功. ...
- 菜鸟的Xamarin.Forms前行之路——从新建项目到APP上架各种报错问题解决方法合集(不定时更新)
出自:博客园-半路独行 原文地址:http://www.cnblogs.com/banluduxing/p/7425791.html 本文出自于http://www.cnblogs.com/banlu ...
- 菜鸟的Xamarin.Forms前行之路——共享组件
出自:博客园-半路独行 本文出自于http://www.cnblogs.com/banluduxing 转载请注明出处. Url Description Xamarin.Social The Xama ...
- Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面
今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...
- Xamarin.Forms 开发资源集合(复制)
复制:https://www.cnblogs.com/mschen/p/10199997.html 收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 S ...
- Xamarin.Forms 开发资源集合
收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 Snppts: Xamarin Forms UI Snippets. Prebuilt Templat ...
随机推荐
- 使用Tor创建.onion域名网站(创建暗网服务和暗网的网站)
使用Tor 的.onion域名创建匿名服务器 Tor不仅可以提供客户端的匿名访问,Tor还可以提供服务器的匿名.通过使用Tor网络,用户可以维护位置不可知的服务器.当然如果要访问这个隐蔽的服务,客户端 ...
- 3DMAX 处理反面
问题起源:从3DMAX导出一个模型为FBX后,在U3D中看到模型很奇怪的透视了,能看到背面看不到正面,这不法线问题,而是面反了. 即然是面反了,为什么在MAX中看起来是正确的呢? 应该是开启了双面模式 ...
- STL - Vector迭代器简单应用之计算元素和
Description 用vector向量容器装入10个整数,然后,使用迭代器iterator和accumulate算法统计出这10个元素的和 Solution #include "stda ...
- tree的所有节点都勾选上或者取消勾选
还有一个功能,就是让tree的所有节点都勾选上或者取消勾选,在api中找了一下有一个方法: check target 选中指定节点. 那我们只能是选中根节点后,实现全选. getRoot none 获 ...
- Centos 安装旧版php5.2
# yum remove php-* # cd /root/ && mkdir new_php && cd new_php # wget -r http://yum.m ...
- 让你的网站用上https
一般申请了SSL证书,会有安装教程教你一步步配置.这里照搬官方教程. 下载得到的 www.domain.com.zip 文件,解压获得3个文件夹,分别是Apache.IIS.Nginx 服务器的证书文 ...
- java基础强化——深入理解java注解(附简单ORM功能实现)
目录 1.什么是注解 2. 注解的结构以及如何在运行时读取注解 2.1 注解的组成 2.2 注解的类层级结构 2.3 如何在运行时获得注解信息 3.几种元注解介绍 3.1 @Retention 3.2 ...
- 【CodeForces - 235C】Cyclical Quest 【后缀自动机】
题意 给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和. 分析 对于s1建后缀自动机.对于询问的每个字符串s2,我们按照处理循环串的 ...
- Tp3.1 文件上传到七牛云
TP3.1 中不支持Composer 就无法用composer 安装 下载历史的SDK https://github.com/qiniu/php-sdk/releases/tag/v7.0.8 下载下 ...
- HRESULT:0x80070057 (E_INVALIDARG)
笔记本蓝屏后,在vs2010中调试项目时出现该异常, 解决方法:清空C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Fi ...