Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Epicalsoft: Xamarin] Fuentes personalizadas en aplicaciones Xamarin.Forms
using Xamarin.Forms;
namespace _20160228.Controls
{
public class AppLabel : Label
{
public static readonly BindableProperty FontNameProperty =
BindableProperty.Create<AppLabel, string>(
p => p.FontName, null, BindingMode.TwoWay);
public string FontName
{
get
{
return (string)GetValue(FontNameProperty);
}
set
{
SetValue(FontNameProperty, value);
}
}
}
}
using _20160228.Controls;
using _20160228.Droid.Renderers;
using Android.Graphics;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(AppLabel), typeof(AppLabelRenderer))]
namespace _20160228.Droid.Renderers
{
public class AppLabelRenderer : LabelRenderer
{
public AppLabelRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (null != Control)
{
var fontName = ((AppLabel)e.NewElement).FontName;
Control.Typeface = TryGetFont(fontName);
}
}
private Typeface TryGetFont(string fontName)
{
try
{
if (fontName == "MagistralC")
return Typeface.CreateFromAsset(Forms.Context.Assets, "MagistralC.otf");
if (fontName == "Radical Beat")
return Typeface.CreateFromAsset(Forms.Context.Assets, "RadicalBeat.ttf");
else
return Typeface.Default;
}
catch (Exception)
{
return Typeface.Default;
}
}
}
}
using _20160228.Controls;
using _20160228.iOS.Renderers;
using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(AppLabel), typeof(AppLabelRenderer))]
namespace _20160228.iOS.Renderers
{
public class AppLabelRenderer : LabelRenderer
{
public AppLabelRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (null != Control)
{
var fontName = ((AppLabel)e.NewElement).FontName;
Control.Font = TryGetFont(fontName, (float)e.NewElement.FontSize);
}
}
private UIFont TryGetFont(string fontName, float fontSize)
{
try
{
return UIFont.FromName(fontName, fontSize);
}
catch (Exception)
{
var defaultFontName = UIFont.FontNamesForFamilyName(UIFont.FamilyNames[0])[0];
return UIFont.FromName(defaultFontName, fontSize);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Código omitido por legibilidad -->
<key>UIAppFonts</key>
<array>
<string>Fonts/MagistralC.otf</string>
<string>Fonts/RadicalBeat.ttf</string>
</array>
</dict>
</plist>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:_20160228.Controls"
x:Class="_20160228.Views.MainPage">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label Text="Magistral" FontSize="Large"/>
<controls:AppLabel Text="Reach" FontSize="Small" FontName="MagistralC"/>
<controls:AppLabel Text="Reach" FontSize="Medium" FontName="MagistralC"/>
<controls:AppLabel Text="Reach" FontSize="Large" FontName="MagistralC"/>
<Label Text="Radical Beat" FontSize="Large"/>
<controls:AppLabel Text="Reach" FontSize="Small" FontName="Radical Beat"/>
<controls:AppLabel Text="Reach" FontSize="Medium" FontName="Radical Beat"/>
<controls:AppLabel Text="Reach" FontSize="Large" FontName="Radical Beat"/>
</StackLayout>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment