Michael Ridland: FreshMvvm 1.0 Preview Release

Since the first release of FreshMvvm it’s been a wild ride, the up take of FreshMvvm has been very impressive it’s really good to see others having success using FreshMvvm. While I’ve been using and loving FreshMvvm for a long long time, even since before it was called FreshMvvm, it’s great to see others also enjoying […]

The post FreshMvvm 1.0 Preview Release appeared first on Michael Ridland.

Details

Xamarin: Los Angeles Department of Building and Safety Goes Mobile with Xamarin

The Los Angeles Department of Buildings and Safety (LADBS) protects the lives and safety of Los Angeles visitors and residents by helping customers comply with codes and laws. In a city of nearly four million people, the department serves more than a million customers and issues more than 140,000 permits a year, performing an average […]

The post Los Angeles Department of Building and Safety Goes Mobile with Xamarin appeared first on Xamarin Blog.

Details

Xamarin: Sport: A Beautiful Open Source Consumer App

Xamarin employees love a good competition! During breaks, we love to challenge each other to a few games of ping-pong or darts. We needed a way to track the best players for each game in our office, so we thought it would be a great opportunity to develop an app to solve the issue. Sport is […]

The post Sport: A Beautiful Open Source Consumer App appeared first on Xamarin Blog.

Details

Gerald Versluis: Using custom fonts on iOS and Android with Xamarin.Forms

Although I wouldn’t recommend doing it too much – you should avoid it to create a most consistent user experience – there are certain scenarios in which you would want to show a custom font.
In a recent project, this is something I needed to do! Being the nice guy that I am, I won’t keep this from you, so here goes.

Custom font on iOS

On iOS it was surprisingly easy actually.

First thing you need to do is incorporate the font you want to use in your project. Under Resources create a folder Fonts (not necessary but it is a but more organised) and put one or more fonts under there in ttf format.  Note; every font is loaded into memory at app start so choose your fonts wisely!

Custom fonts
Custom fonts

Open your info.plist in your favorite text editor and at this section.

<key>UIAppFonts</key>
<array>
   <string>Fonts/Lato-Regular.ttf</string>
   <string>Fonts/Stackyard.ttf</string>
   <string>Fonts/ComicSaaaaaans.ttf</string>
</array>

Of course, if you didn’t put them in a fonts subfolder leave out the ‘Fonts/’ part.

From there you can use the iOS Appearance API to apply your font to whatever you want. In my case this was the navigation bar, which resulted in this:

UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes
{
   TextColor = UIColor.White,
   Font = UIFont.FromName("Stackyard PERSONAL USE", 32)
});

Navigationbar iOS custom font
Navigationbar iOS custom font

Note that you have to use the fonts name, and not the filename. So be sure to double check that!

When you are using Xamarin.Forms you can apply it to labels even easier, just use the FontFamily property.

<!-- Omitted rest of page markup -->
<Label Grid.Row="1" Text="Bier &amp; Pizza" FontSize="60" HorizontalTextAlignment="Center" TextColor="{x:Static static:Constants.KembitOrange}"
           FontFamily="Stackyard PERSONAL USE" StyleId="Stackyard" />
<!-- Omitted rest of page markup -->

Which, in turn results looking like this.

Xamarin.Forms Label custom font
Xamarin.Forms Label custom font

If you did pay attention you saw that I didn’t only use the FontFamily property, but I have also set the StyleId to something that has to do with a custom font. Which brings us to the Android side of things.

Custom font on Android

On Android things are a bit different and also a bit more complicated, but no worries nothing we can’t handle.
Again we start with also adding your fonts to the project, here don’t add them under Resources but under Assets.

Custom fonts Android
Custom fonts Android

Now what we need to do is create a CustomRenderer for the controls in which we want to show our fonts! If you need a reminder of how CustomRenderers worked, look at my earlier post here.

A obvious choice is the Label control. When we define a renderer with this code overriding the ElementChanged we can use the StyleId, like you’ve seen above, to specify which font to use right from our shared code!

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
   base.OnElementChanged(e);

   if (!string.IsNullOrEmpty(e.NewElement?.StyleId))
   {
      var font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, e.NewElement.StyleId + ".ttf");

       Control.Typeface = font;
    }
}

Memory-wise you would probably be better off creating the fonts in one place and not for every label. It is here shown to keep the code compact.

Also note, that here – unlike iOS – you do have to use the font filename here!

To be complete, here is the not so surprising outcome;

Label custom font Android
Label custom font Android

There is probably some way to achieve this with Android theme’s but I didn’t dig into that yet.
If you have, please let me know how! Especially how to set a custom font on the Navigationbar in Android.. That has been keeping me up a while now.

Also Windows Phone isn’t included here. Sorry!

Details

Adam J Wolf: Weekly Xamarin Newsletter Issue #75

Geolocation for iOS, Android, and Windows Made Easy Pierce Boggan, from Xamarin Inc., shows how easy it is to add Geolocation to your apps. LayoutTo doesn’t do what you think it does Jason Smith, the father of Xamarin.Forms, admits he screwed up! Custom layouts with Xamarin.Forms, Part 1 Jason Smith, the father of Xamarin.Forms, starts […]

The post Weekly Xamarin Newsletter Issue #75 appeared first on Syntax is my UI.

Details