菜鸟的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 ...
随机推荐
- 关于java中的OutOfMemory种类和解决方法
1.OutOfMemory的三种情况 1) 永久区溢出 Exception in thread "main" java.lang.OutOfMemoryError: PermGen ...
- delphi XE8 Android SDK SET
delphi XE Berlin Android SDK SET RAD>Tools>Options>SDk Manager> https://dl-ssl.google.co ...
- Python基础学习九 单元测试
import unittest import HTMLTestRunner #产生测试报告 from BeautifulReport import BeautifulReport def calc(x ...
- PHP - 用户异常断开连接,脚本强制继续执行,异常退出回调
试想如下情况.如果你的用户正在执行一个需要非常长的执行时间的操作.他点了执行了之后,浏览器就开始蛋疼地转.如果执行5分钟,你猜他会干啥,显然会觉得什么狗屎垃圾站,这么久都不响应,然后就给关了.当然这个 ...
- Python基础:Python数据类型及逻辑判断语句
Python代码需要严谨的缩进 # 导包 import random # ********************输入输出***************** # 输出 print("hell ...
- JAVA集合中的迭代器的遍历
JAVA中的迭代器,迭代实质上就是遍历,在JAVA中使用iterator()方法进行迭代.需要注意的是,iterator()方法的返回值是Iterator对象.Iterator对象有三个方法,hasN ...
- 如何了解一个Web项目
一:学会如何读一个JavaWeb项目源代码 步骤:表结构->web.xml->mvc->db->spring ioc->log->代码 1.先了解项目数据库的表结构 ...
- 代码质量检测-Sonar
一. Sonar简介 sonarqube系统是一个代码质量检测工具 由以下四个组件组成(https://docs.sonarqube.org/display/SONAR/Architecture+an ...
- 浅谈svn的hook机制
一.什么是钩子 所谓svn的hook机制,就是用户在管理数据仓库的时候,当特定的事件发生时,相应的hook会被调用,hook 其实就相当于特定事件的处理函数. 当前 Subversion 提供了5种可 ...
- c++ std::unordered_set
std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...