aaa转16进制 ios
2016-09-12 12:48:06 NickYangBooy 阅读数 283
UIColor+ZXLacy.h

#import


@interface UIColor (ZXLazy)


+ (UIColor *)colorWithRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue;


+ (UIColor *)colorWithHexString:(NSString *)color;

 

@end





UIColor+ZXLazy.m


#import "UIColor+ZXLazy.h"


@implementation UIColor (ZXLazy)


+ (UIColor *)colorWithRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue

{

    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];

}



+ (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];



}

@end

2015-02-21 15:45:59 yamingwu 阅读数 489

在代码中添加以下宏定义:

//color
#define RGB(r, g, b)             [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define RGBAlpha(r, g, b, a)     [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]

使用:

self.window.backgroundColor = HexRGB(0x187eb4);
self.window.backgroundColor = HexRGBAlpha(0x187eb4,0.5); 
self.window.backgroundColor = RGB(200, 130, 10);
self.window.backgroundColor = RGBAlpha(200, 130, 10,0.4);


2014-07-11 15:12:54 assholeu 阅读数 648

16进制转UIColor需要按位转换成RGB

- (UIColor *)RGBColor:(unsigned int)hexColor{
    
    unsigned int red,green,blue;

    red = hexColor>>16&0xff;
    green = hexColor>>8&0xff;
    blue = hexColor&0xff;
    
    return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:1.0f];
}

使用方法很简单,如下:

[self RGBColor:0x1f1f1f]

HAVE FUN!!!

2017-11-23 13:45:00 weixin_33981932 阅读数 10
-(NSString *)getName:(unsigned char *)data
{
    NSStringEncoding encodingGB18030 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *name = [NSString stringWithCString:(const char *)data encoding:encodingGB18030];
    
    return name;
}

 

转载于:https://my.oschina.net/mexiaobai1315/blog/1577859

2016-06-01 15:34:00 weixin_33922672 阅读数 3
// 将字符串转为16进制
+ (NSString *)hexStringFromString:(NSString *)string {
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"%@",myD);
    Byte *bytes = (Byte *)[myD bytes];

    //下面是Byte 转换为16进制。
    NSString *hexStr = @"";
    for(int i = 0; i < [myD length]; i++) {
//        NSLog(@"%x",bytes[i]);
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; //16进制数
        if([newHexStr length] == 1) {
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        } else {
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
        }
    }
    int num = hexStr.length % 16;
    if (num == 0) {

    } else {
        for (int i = 0; i < 16 - num; i++) {
            hexStr = [NSString stringWithFormat:@"%@F",hexStr];
        }
    }
    return hexStr;
}

 

转载于:https://my.oschina.net/zsyzone/blog/685489

ios 10进制转16进制

阅读数 391

没有更多推荐了,返回首页