Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIColor from Hex in Monotouch

How to get a UIColor from an hex value in Monotouch?

like image 491
Luis Avatar asked Apr 25 '12 07:04

Luis


2 Answers

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); 
like image 94
Luis Avatar answered Oct 05 '22 18:10

Luis


Maybe this helps you, if you are using Xamarin.Forms:

using Xamarin.Forms; using Xamarin.Forms.Platform.iOS;  ... Color.FromHex("#00FF00").ToUIColor(); 
like image 37
alexcons Avatar answered Oct 05 '22 18:10

alexcons