16进制颜色与UIColor互转

代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
    view.backgroundColor=[self colorWithHexString:@"e26562"];
    [self.view addSubview:view];

}
#pragma -mark -functions
//16进制颜色#e26562与UIColor互转,设置View背景颜色
- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;

    //r
    NSString *rString = [cString substringWithRange:range];

    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
时间: 2024-11-04 23:28:10

16进制颜色与UIColor互转的相关文章

iOS工具种之16进制颜色转为UIColor

 #define DEFAULT_VOID_COLOR [UIColor whiteColor] + (UIColor *)colorWithHexString:(NSString *)stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

[iOS]把16进制颜色转换UIColor

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIColor *color = [self colorWithHexString:@"#81c52b"]; [self.view setBackgroundColor:color]; } - (UIColor *) colorWithHexString:

16进制颜色字符串转为UIColor

//16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be

iOS开发中16进制颜色(html颜色值)字符串转为UIColor

//16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be

iOS开发-从16进制颜色中获取UIColor

目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface TextServcie : NSObject +(UIColor *) getColorFromHEX:(NSString *)hex; @end .m

swift - 16进制颜色扩展(1.支持# 2.支持不带# , 3支持带0X)

/** * 设置16进制颜色: * 可识别类型 * 1:有# * 2:没有# * 3:含有0X */ extension UIColor{ class func hexadecimalColor(hexadecimal:String)->UIColor{ var cstr = hexadecimal.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString; if(cstr.lengt

随机16进制颜色生成方法

function colorOb() { var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];var sum =  "";for (var i = 0; i < 6; i++) {//向上取整,得到进制数var num= Math.ceil(Math.random() * 16);sum += arr[id];var sym =  "#&qu

C# RGB与16进制颜色转换方法

http://www.cnblogs.com/goldarch/archive/2010/08/16/1801053.html #region [颜色:16进制转成RGB]        /// <summary>        /// [颜色:16进制转成RGB]        /// </summary>        /// <param name="strColor">设置16进制颜色 [返回RGB]</param>       

前端笔试题:拍扁多维数组、提取URL中的参数变成对象、实现string的trim()函数、判断Email地址、16进制颜色转rgb格式

(1)拍扁数组 这是一个著名互联网公司今天的前端笔试题:多维数组维数不定,如[2,4,1,[2,3,4,[1,2],5,3],3,8]拍扁为[2,4,1,2,3,4,1,2,5,3,3,8].维数不定,应该要用递归: 答案: function flatten(arr){ var arrLength=arr.length; for(var i=0;i<arrLength;i++){ if(arr[i].constructor==Array){ arr.splice(i,1,flatten(arr[