¿Cómo obtener un UIColor de un valor hexadecimal en Monotouch?UIColor de Hex en Monotouch
Respuesta
He encontrado algunas soluciones para Objective C y ninguno específicamente para MonoTouch Terminé el desarrollo de un método de extensión basado en la solución más popular para 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)
);
}
}
y utilizar de esta manera:
new UIColor().FromHex(0x4F6176);
Actualización, parece que como fuera de Monotouch 5.4 UIColor no tiene un constructor sin parámetros así que úsala así:
UIColor.Clear.FromHex(0xD12229);
Aquí uno que le permite utilizar una cadena como en CSS:
UIColor textColorNormal = UIColor.Clear.FromHexString("#f4f28d", 1.0f);
Y aquí es la clase:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Globalization;
namespace YourApp
{
public static class UIColorExtensions
{
public static UIColor FromHexString (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)), 16)/255f;
green = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(1, 1)), 16)/255f;
blue = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(2, 1)), 16)/255f;
return UIColor.FromRGBA(red, green, blue, alpha);
}
case 6 : // #RRGGBB
{
red = Convert.ToInt32(colorString.Substring(0, 2), 16)/255f;
green = Convert.ToInt32(colorString.Substring(2, 2), 16)/255f;
blue = Convert.ToInt32(colorString.Substring(4, 2), 16)/255f;
return UIColor.FromRGBA(red, green, blue, alpha);
}
default :
throw new ArgumentOutOfRangeException(string.Format("Invalid color value {0} is invalid. It should be a hex value of the form #RBG, #RRGGBB", hexValue));
}
}
}
}
Super útil! Me estoy comunicando con un servicio web que almacena todo exactamente como css. ¡Gracias por tu publicación! – BRogers
¡Sin preocupaciones fue divertido de entrenar! – superlogical
respuesta de alexcons: https://stackoverflow.com/a/41046485/2539616 ... debajo está el ... Color.FromHex ("# 00FF00"). ToUIColor(); – Adam
Tal vez esto le ayuda, si está utilizando Xamarin.Forms:
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
...
Color.FromHex("#00FF00").ToUIColor();
Esta debería ser la respuesta aceptada, no es necesario reinventar la rueda. – MonkeyCoder
En segundo lugar, esta es la respuesta. Crear una clase para hacer esto es exagerado. Pero eso fue a partir de 2012. – Adam
Esto es útil en xamarin.forms, pero si está trabajando en xamarin.ios, entonces no es algo que haría en las bibliotecas de plataforma de xamarin.forms. –
Como una opción, por ejemplo
public static UIColor FromHEX(string hex)
{
int r = 0, g = 0, b = 0, a = 0;
if (hex.Contains("#"))
hex = hex.Replace("#", "");
switch (hex.Length)
{
case 2:
r = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
g = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
b = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
a = 255;
break;
case 3:
r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
a = 255;
break;
case 4:
r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
a = int.Parse(hex.Substring(3, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
break;
case 6:
r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
a = 255;
break;
case 8:
r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
a = int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
break;
}
return UIColor.FromRGBA(r, g, b, a);
}
- 1. Cómo convertir códigos de color HEX RGB a UIColor?
- 2. Valor HEX en Objective-C
- 3. UIColor clearColor
- 4. color hexadecimal de UIColor
- 5. Extrayendo rgb de UIColor
- 6. Aligerar un UIColor
- 7. Convierta CGcolorRef a UIColor
- 8. Parse HEX ASCII en números?
- 9. Ascii/Hex convertir en bash
- 10. Hex-Value en Visual Basic
- 11. Char to Hex en javascript
- 12. MonoTouch: uncaughtExceptionHandler?
- 13. cómo convertir nsstring a uicolor?
- 14. UIColor a hexadecimal (color web)
- 15. ¿Por qué usar hex?
- 16. C++ Hex Parsing
- 17. Convertir hex a flotante
- 18. bash ascii al hex
- 19. Convertir RBG a HEX
- 20. NSString (hex) a bytes
- 21. Decode Hex de cadenas en Python 3
- 22. Cómo cargar un UIColor de un Plist
- 23. Cómo subclase UIApplication en Monotouch?
- 24. aync y espera en monotouch
- 25. Debug.WriteLine equivalente en MonoTouch/MonoDroid?
- 26. Cómo implementar UISearchBar en MonoTouch
- 27. ¿Cómo defino los valores constantes de UIColor?
- 28. UIColor colorWithPatternImage: problemas de transparencia en separaratorColor de UITableView
- 29. Convertir una cadena a HEX en SQL
- 30. matriz Hex a char en C
¿Dónde colocas esto? ¿Creas una nueva clase 'UIColorExtensions'? – testing
¿No puedo hacer algo como una categoría en Objective-C? –
alexcons respuesta: https://stackoverflow.com/a/41046485/2539616 ... a continuación está el que ... Color.FromHex ("# 00FF00"). ToUIColor(); – Adam