码迷,mamicode.com
首页 > 移动开发 > 详细

iOS RGB颜色封装

时间:2015-09-18 13:26:17      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

使用类别创建

.h文件

#import <UIKit/UIKit.h>

@interface UIColor (HexColor)

+ (UIColor *)colorWithHex:(NSString *)hex;

@end

.m文件

#import "UIColor+HexColor.h"

@implementation UIColor (HexColor)

+ (UIColor *)colorWithHex:(NSString *)hex {
    unsigned int red, green, blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    if (hex.length == 7) {
        range.location = 1;
    }
    [[NSScanner scannerWithString:[hex substringWithRange:range]]scanHexInt:&red];
    range.location += 2;
    [[NSScanner scannerWithString:[hex substringWithRange:range]]scanHexInt:&green];
    range.location += 2;
    [[NSScanner scannerWithString:[hex substringWithRange:range]]scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red / 255.0) green:(float)(green / 255.0) blue:(float)(blue / 255.0) alpha:1.0];
}

@end

 

iOS RGB颜色封装

标签:

原文地址:http://www.cnblogs.com/DWdan/p/4818779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!