UI颜色扩展
UIColor extension
我在 objective-c 中扩展了 UIColor class
头文件是这样的:
@interface UIColor (ColorDecision)
+ (UIColor*) directOrLegacyWithColor;
+ (UIColor*) changeColor;
@end
实施:
#import "UIColor+ColorDecision.h"
@implementation UIColor (ColorDecision)
+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}
+ (UIColor *)changeColor{
UIColor *newColor;
if (self == Color_C7) newColor = Color_Direct;
return newColor;
}
@end
我在以下行中收到来自编译器的警告“Incompatible pointer types assigning to 'UIColor *' from 'Class'”
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
我知道这个问题与指针有一些关系,但我不知道该怎么办。
编辑
感谢 manishharma93 的评论,我现在解决了第一个问题,当我想使用指定的方法时,似乎方法不可见。
我在这样的全局头文件中定义了我的颜色:
#define Color_C7 [SharedUtility colorWithHexString:@"FF6A00"] // orange
#define Color_Direct [Utility colorWithHexString:@"313d53"] // Color for direct
这是Utility中'colorWithHexString'函数的实现class:
+(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 = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
green = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
blue = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
red = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
green = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
blue = [SharedUtility colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
green = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
blue = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
red = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
green = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
blue = [SharedUtility colorComponentFrom: colorString start: 6 length: 2];
break;
default:
LOG(@"WARNING: tried to set color from string: %@ BUT string should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB ", hexString);
return nil;
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
现在,当我想像这样使用我的扩展功能时:
self.background = Color_C7。直接OrLegacyWithColor
'directOrLegacyWithColor' 函数不可见
在您的方法中,将 "self" 更改为“[UIColor self]”。
+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}
我在 objective-c 中扩展了 UIColor class 头文件是这样的:
@interface UIColor (ColorDecision)
+ (UIColor*) directOrLegacyWithColor;
+ (UIColor*) changeColor;
@end
实施:
#import "UIColor+ColorDecision.h"
@implementation UIColor (ColorDecision)
+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}
+ (UIColor *)changeColor{
UIColor *newColor;
if (self == Color_C7) newColor = Color_Direct;
return newColor;
}
@end
我在以下行中收到来自编译器的警告“Incompatible pointer types assigning to 'UIColor *' from 'Class'”
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
我知道这个问题与指针有一些关系,但我不知道该怎么办。
编辑
感谢 manishharma93 的评论,我现在解决了第一个问题,当我想使用指定的方法时,似乎方法不可见。
我在这样的全局头文件中定义了我的颜色:
#define Color_C7 [SharedUtility colorWithHexString:@"FF6A00"] // orange
#define Color_Direct [Utility colorWithHexString:@"313d53"] // Color for direct
这是Utility中'colorWithHexString'函数的实现class:
+(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 = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
green = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
blue = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
red = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
green = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
blue = [SharedUtility colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
green = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
blue = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
red = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
green = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
blue = [SharedUtility colorComponentFrom: colorString start: 6 length: 2];
break;
default:
LOG(@"WARNING: tried to set color from string: %@ BUT string should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB ", hexString);
return nil;
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
现在,当我想像这样使用我的扩展功能时: self.background = Color_C7。直接OrLegacyWithColor 'directOrLegacyWithColor' 函数不可见
在您的方法中,将 "self" 更改为“[UIColor self]”。
+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}