在Monotouch中从十六进制获取UIColor

33
如何在Monotouch中从十六进制值获取UIColor?
4个回答

47

我在Objective 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);

你把它放在哪里?你创建一个新的UIColorExtensions类吗? - testing
我能不能在Objective-C中创建类似于Category的东西? - I make my mark
以下是alexcons的答案:https://dev59.com/FGkv5IYBdhLWcg3w1kWr#41046485 ... 下面是代码... Color.FromHex("#00FF00").ToUIColor(); - Adam
@Adam alexcons的答案强制你导入表单库,但你可能不想这样做。 - Luis

43

如果您正在使用Xamarin.Forms,那么这可能会对您有所帮助:

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

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

1
这应该是被接受的答案,没有必要重新发明轮子。 - MonkeyCoder
同意,这就是答案。创建一个类来做这件事情有些过度设计了。但那是从2012年的观点出发的。 - Adam
7
这在Xamarin.Forms上很有用,但如果你在Xamarin.iOS上工作,那么从Xamarin.Forms中提取平台库不是我会做的事情。 - Steven Evers

34

这里是一个允许您像在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));

            }
        }
    }   
}

1
超级有用!我正在与一个存储所有内容的 Web 服务进行通信,就像 CSS 一样。谢谢您的发布! - BRogers
1
以下是alexcons的答案:https://dev59.com/FGkv5IYBdhLWcg3w1kWr#41046485 ... 下面是代码... Color.FromHex("#00FF00").ToUIColor(); - Adam

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)

如果您使用带有 alpha 通道的 HEX,请注意它在末尾指示。或者,根据您的需要更正代码的工作方式。 - KIzotov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接