Last active
February 29, 2016 04:05
-
-
Save malexandersalazar/62bdf1ac153078817b26 to your computer and use it in GitHub Desktop.
[Epicalsoft: Xamarin] Fuentes personalizadas en aplicaciones Xamarin.Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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