删除UIColor分类
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
//
|
||||
// UIColor+Extension.h
|
||||
// PlanetStar
|
||||
//
|
||||
// Created by Runyalsj on 2020/6/16.
|
||||
// Copyright © 2020 WUJIE INTERACTIVE. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface UIColor (Extension)
|
||||
+ (UIColor *) colorWithHexString:(NSString *) hexString;
|
||||
+ (UIColor *) colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha;
|
||||
+ (NSString *) hexStringFromColor:(UIColor *)color;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@@ -1,102 +0,0 @@
|
||||
//
|
||||
// UIColor+Extension.m
|
||||
// PlanetStar
|
||||
//
|
||||
// Created by Runyalsj on 2020/6/16.
|
||||
// Copyright © 2020 WUJIE INTERACTIVE. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UIColor+Extension.h"
|
||||
|
||||
@implementation UIColor (Extension)
|
||||
+ (UIColor *) colorWithHexString: (NSString *) hexString {
|
||||
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
|
||||
CGFloat alpha, red, blue, green;
|
||||
switch ([colorString length]) {
|
||||
case 3: // #RGB
|
||||
alpha = 1.0f;
|
||||
red = [self colorComponentFrom: colorString start: 0 length: 1];
|
||||
green = [self colorComponentFrom: colorString start: 1 length: 1];
|
||||
blue = [self colorComponentFrom: colorString start: 2 length: 1];
|
||||
break;
|
||||
case 4: // #ARGB
|
||||
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
|
||||
red = [self colorComponentFrom: colorString start: 1 length: 1];
|
||||
green = [self colorComponentFrom: colorString start: 2 length: 1];
|
||||
blue = [self colorComponentFrom: colorString start: 3 length: 1];
|
||||
break;
|
||||
case 6: // #RRGGBB
|
||||
alpha = 1.0f;
|
||||
red = [self colorComponentFrom: colorString start: 0 length: 2];
|
||||
green = [self colorComponentFrom: colorString start: 2 length: 2];
|
||||
blue = [self colorComponentFrom: colorString start: 4 length: 2];
|
||||
break;
|
||||
case 8: // #AARRGGBB
|
||||
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
|
||||
red = [self colorComponentFrom: colorString start: 2 length: 2];
|
||||
green = [self colorComponentFrom: colorString start: 4 length: 2];
|
||||
blue = [self colorComponentFrom: colorString start: 6 length: 2];
|
||||
break;
|
||||
default:
|
||||
[NSException raise:@"Invalid color value" format: @"Color value %@ is invalid. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
|
||||
break;
|
||||
}
|
||||
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
|
||||
}
|
||||
|
||||
+ (UIColor *) colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
|
||||
if (hexString.length == 0) return [UIColor blackColor];
|
||||
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
|
||||
CGFloat red, blue, green;
|
||||
switch ([colorString length]) {
|
||||
case 3: // #RGB
|
||||
red = [self colorComponentFrom: colorString start: 0 length: 1];
|
||||
green = [self colorComponentFrom: colorString start: 1 length: 1];
|
||||
blue = [self colorComponentFrom: colorString start: 2 length: 1];
|
||||
break;
|
||||
case 4: // #ARGB
|
||||
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
|
||||
red = [self colorComponentFrom: colorString start: 1 length: 1];
|
||||
green = [self colorComponentFrom: colorString start: 2 length: 1];
|
||||
blue = [self colorComponentFrom: colorString start: 3 length: 1];
|
||||
break;
|
||||
case 6: // #RRGGBB
|
||||
red = [self colorComponentFrom: colorString start: 0 length: 2];
|
||||
green = [self colorComponentFrom: colorString start: 2 length: 2];
|
||||
blue = [self colorComponentFrom: colorString start: 4 length: 2];
|
||||
break;
|
||||
case 8: // #AARRGGBB
|
||||
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
|
||||
red = [self colorComponentFrom: colorString start: 2 length: 2];
|
||||
green = [self colorComponentFrom: colorString start: 4 length: 2];
|
||||
blue = [self colorComponentFrom: colorString start: 6 length: 2];
|
||||
break;
|
||||
default:
|
||||
[NSException raise:@"Invalid color value" format: @"Color value %@ is invalid. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
|
||||
break;
|
||||
}
|
||||
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
|
||||
}
|
||||
|
||||
|
||||
+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
|
||||
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
|
||||
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
|
||||
unsigned hexComponent;
|
||||
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
|
||||
return hexComponent / 255.0;
|
||||
}
|
||||
|
||||
+ (NSString *)hexStringFromColor:(UIColor *)color {
|
||||
const CGFloat *components = CGColorGetComponents(color.CGColor);
|
||||
|
||||
CGFloat r = components[0];
|
||||
CGFloat g = components[1];
|
||||
CGFloat b = components[2];
|
||||
|
||||
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
|
||||
lroundf(r * 255),
|
||||
lroundf(g * 255),
|
||||
lroundf(b * 255)];
|
||||
}
|
||||
@end
|
Reference in New Issue
Block a user