Created
February 8, 2016 21:27
-
-
Save anthonyadamski/9d69e7222e9ded76ecee to your computer and use it in GitHub Desktop.
Maintaining font styles with shared typography in Xamarin
This file contains hidden or 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 System; | |
using System.Drawing; | |
using System.Runtime.Serialization; | |
using System.Reflection; | |
#if __IOS__ | |
using Foundation; | |
using UIKit; | |
using CoreGraphics; | |
#endif | |
namespace Shared | |
{ | |
static class ExtensionMethods | |
{ | |
#if __IOS__ | |
#region Color | |
public static UIColor ToUIColor (this Color color) { return UIColor.FromRGB (color.R, color.G, color.B); } | |
public static CGColor ToCGColor (this Color color) { return UIColor.FromRGB (color.R, color.G, color.B).CGColor; } | |
#endregion | |
public static void SetFontStyle (this UIKit.UILabel uiLabel, FontStyle fontStyle) { | |
// font | |
uiLabel.Font = fontStyle.UIFont; | |
// color | |
uiLabel.TextColor = fontStyle.Color.ToUIColor(); | |
} | |
public static void SetFontStyle (this UIKit.UIButton uiButton, FontStyle fontStyle) { | |
// font | |
uiButton.Font = fontStyle.UIFont; | |
// color | |
uiButton.SetTitleColor (fontStyle.Color.ToUIColor(), UIControlState.Normal); | |
} | |
#endif | |
#if __ANDROID__ | |
public static Android.Graphics.Color ToAndroidColor (this Color color) { | |
return new Android.Graphics.Color (color.R, color.G, color.B); | |
} | |
public static void SetFontStyle (this Android.Widget.TextView textView, FontStyle fontStyle) { | |
// font | |
textView.Typeface = fontStyle.Typeface; | |
// color | |
textView.SetTextColor (fontStyle.Color.ToAndroidColor ()); | |
// size | |
textView.TextSize = fontStyle.FontSize; | |
} | |
#endif | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment