2019-08-20 11:35:42 syzd451529064 阅读数 65

// 16进制转换成UIColor

  • (UIColor *)colorWithHexString:(NSString *)hexColor alpha:(float)opacity {
    NSString * cString = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    return [UIColor blackColor];
    // 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 blackColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString * rString = [cString substringWithRange:range];
    range.location = 2;
    NSString * gString = [cString substringWithRange:range];
    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:opacity];
    }

// UIColor转换成16进制颜色值

  • (NSString *)hexString {
    //颜色值个数,rgb和alpha
    NSInteger component = CGColorGetNumberOfComponents(self.CGColor);
    const CGFloat *components = CGColorGetComponents(self.CGColor);
    CGFloat red = components[0];//红色
    CGFloat green = components[1];//绿色
    CGFloat blue = components[2];//蓝色
    if (component == 4) {
    CGFloat alpha = components[3];//透明度
    return [NSString stringWithFormat:@"#%02lX%02lX%02lX%02lX", lroundf(alpha * 255), lroundf(red * 255), lroundf(green * 255), lroundf(blue * 255)];
    } else {
    return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(red * 255), lroundf(green * 255), lroundf(blue * 255)];
    }
    }
2016-05-09 22:48:49 l2i2j2 阅读数 626
- (void)RGBWithHexColor:(int)hexColor {
    int red = ((hexColor & 0xFF0000) >> 16);
    int green = ((hexColor & 0xFF00) >> 8);
    int blue = (hexColor & 0xFF);
    NSLog(@"red:%d  green:%d  blue:%d", red, green, blue);
}

2016-04-05 15:43:39 qq_26705543 阅读数 781

一、整形装换成两个字节

    

    int value = 456;

    unsigned char  byte1 = (value & 0xff00)>>8//8

    unsigned char  byte2 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x ",byte1,byte2);

  

        byte1 = 1;  byte2= c8;

二、两个字节转换成十进制整数

    char bytes[]={byte1,byte2};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    int   temp  = (by2|(by1<<8));

    NSLog(@"temp = %d",temp);

    

    temp = 456;

三、整形装换成三个字节

    int value =134456;

    unsigned char  byte1 = (value & 0xff0000)>>16;//最高8

    unsigned char  byte2 = (value & 0xff00)>>8;//中间8

    unsigned char  byte3 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x byte3= %x ",byte1,byte2,byte3);

    byte1= 2   byte2= d byte3= 38


四、三个字节转换成十进制整数

    char bytes[]={byte1,byte2,byte3};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    unsigned char  by3 = (bytes[2] & 0xff);//8

    

    int   temp  = (by3|(by2<<8)|(by1<<16));

    NSLog(@"temp = %d",temp);

   temp = 134456

如果还有更多的自己接需要转换以此类推

2015-10-25 20:20:46 wang1018960145 阅读数 644

 无论什么样的开发语言都会遇到进制的转换,十进制转16进制,16进制转汉字,10进制转汉字,,,在银行项目中尤其用到16进制的转换,因为后台发来的报文多是16进制的,,,

+ (NSData *)chineseToHex:(NSString*)chinnese {
    NSStringEncoding encodingGB18030 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//自定义转换格式
    NSData *reponseData = [chinnese dataUsingEncoding:encodingGB18030];
    
    return reponseData;
}
返回的是data类型的

2017-10-18 17:14:31 qq_15963233 阅读数 289

右键点击new file--》选择objective-c file--》创建类 category--》起个名字HexadecimalColor--》class处选择UIColor--》点击create--》

.h文件实现方法--》

#import <UIKit/UIKit.h>

@interface UIColor (HexadecimalColor)

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

@end



--》在.m文件中实现方法具体如下--》


#import "UIColor+HexadecimalColor.h"


@implementation UIColor (HexadecimalColor)

//实现方法

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha

{

    //删除字符串中的空格

    //    NSString*cString = [[color stringByTrimmingCharactersInSet[NSCharacterSet         whitespaceAndNewlineCharacterSet]] uppercaseString];

    NSString*cString = [color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    // String should be 6 or 8 characters

    if ([cString length] < 6)

    {

        return [UIColor clearColor];

    }

    // strip 0X if it appears

    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾

    

    

    if ([cString hasPrefix:@"0X"])

    {

        cString = [cString substringFromIndex:2];

    }

    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾

    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:alpha];

}

@end





ios扩展16进制颜色

阅读数 8355

ios进制相互转换

阅读数 1583

iOS 16进制转UIColor

阅读数 665

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

博文 来自: assholeu
没有更多推荐了,返回首页