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)), ...
Get Xamarin Blueprints now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.