O'Reilly logo

Xamarin Blueprints by Michael Williams

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extending the UIColor framework

In this section we are going to apply a common technique for extending on standard iOS classes. In the UIColor class, there is no function for applying hex strings to determine a color, so let's add this on top. Create a new folder called Extensions, add in a new file called UIColorExtensions.cs, and implement the following:

public static class UIColorExtensions { public static UIColor FromHex(this UIColor color, string hexValue, float alpha = 1.0f) { var colorString = hexValue.Replace("#", ""); if (alpha > 1.0f) { alpha = 1.0f; } else if (alpha < 0.0f) { alpha = 0.0f; } float red, green, blue; switch (colorString.Length) { case 3: // #RGB { red = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(0, 1)), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required