从的UIColor在十六进制MonoTouch的(UIColor from Hex in Monot

2019-06-23 22:42发布

如何从在MonoTouch中的十六进制值的UIColor?

Answer 1:

我发现目标C,没有专门为MonoTouch的我结束了开发基于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)
            );
        }
    }

并使用它像这样:

new UIColor().FromHex(0x4F6176);

更新 ,似乎是关闭的MonoTouch 5.4的UIColor没有参数的构造函数,以便使用它像这样:

 UIColor.Clear.FromHex(0xD12229);


Answer 2:

在这里,人们可以让你使用一个字符串像CSS:

UIColor textColorNormal = UIColor.Clear.FromHexString("#f4f28d", 1.0f);

这里是类:

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));

            }
        }
    }   
}


Answer 3:

也许这可以帮助你,如果你是使用Xamarin.Forms:

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

...
Color.FromHex("#00FF00").ToUIColor();


Answer 4:

作为一个选项,例如

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);
    }

xamarin.ios 的UIColor colorhelper



文章来源: UIColor from Hex in Monotouch