How to get a UIColor from an hex value in Monotouch?
I found some solutions for Objective C and none specifically for Monotouch I ended up developing an extension method based on the most popular solution for IOS:
public static class UIColorExtensions { public static UIColor FromHex(this UIColor color,int hexValue) { return UIColor.FromRGB( (((float)((hexValue & 0xFF0000) >> 16))/255.0f), (((float)((hexValue & 0xFF00) >> 8))/255.0f), (((float)(hexValue & 0xFF))/255.0f) ); } }
and use it like this:
new UIColor().FromHex(0x4F6176);
Update, it seems that as off Monotouch 5.4 UIColor does not have a parameterless constructor so use it like this:
UIColor.Clear.FromHex(0xD12229);
Maybe this helps you, if you are using Xamarin.Forms:
using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; ... Color.FromHex("#00FF00").ToUIColor();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With