今天我得到了一项新任务。这是根据给定的值创建单个类来为应用程序设置颜色。举个例子,如果我想设置background_Colour,我必须从下面的代码中获得这个。
[[ThemeManager SharedInstance] setcolor:background_Colour]
但我必须在My singletonClass中使用不同的值(EX: labelColor, FontColor)
。
到目前为止,我已经创建了单例类。但我不知道如何完成这项任务。谁能帮我?这是我到目前为止所尝试的。
ThemeManager.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ThemeManager : NSObject
+(instancetype)sharedInstance;
-(nullable UIColour *)setcolor:(NSString *)Colour;
@end
ThemeManager.m
#import "DAThemManager.h"
@implementation DAThemManager{
UIColor *_customColor;
}
+(instancetype)sharedInstance{
static id sharedManagerInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManagerInstance = [[self alloc] init];
});
return sharedManagerInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
-(nullable UIColour *)setcolor:(NSString *)Colour{
// i dont know what to implement here
// _customColor = [UIColor:redColour key:background_Colour];
return _customColor ;
}
@end
答案 0 :(得分:0)
要自定义类的所有实例的外观,请使用外观来获取类的外观代理。例如,要修改UINavigationBar的所有实例的条形背景色调颜色: [[UINavigationBar外观] setBarTintColor:myNavBarBackgroundColor]; https://developer.apple.com/documentation/uikit/uiappearance
如果字符串值格式化为十六进制,则可以使用:
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha {
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:alpha];
}
答案 1 :(得分:0)
#define RP_GREEN_COLOR [UIColor colorWithRed:62/255.0 green:156/255.0 blue:133/255.0 alpha:1];
//你的颜色在这里
现在创建PrefixHeader.pch文件。 新文件 - &gt; (ios,Other)PCH文件。
还在Target中添加PCH文件名 - &gt;构建设置---&gt;前缀标题 喜欢 - yourProjectName / PrefixHeader.pch
您可以使用此 -
self.view.backgroundColor = RP_GREEN_COLOR;