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 { ...