I want to create a common method in Utility file to set colour for labels in my application. I have different different colours for all labels through application.
How to write common method to set colours?.
I want to create a common method in Utility file to set colour for labels in my application. I have different different colours for all labels through application.
How to write common method to set colours?.
I would recommend is using a category. Make a category called "UIColor+Branding" or something similar. In here, you would have a list of all the colours you need. What you can do then, is simply import that category into your files that need these custom colours import "UIColor+Branding.h"
and you will be able to call [UIColor myCustomColor]
.
UIColor+Branding.h
@interface UIColor (Branding)
+ (UIColor)myCustomColor;
@end
UIColor+Branding.m
#import "UIColor+Branding.h"
@implementation UIColor (Branding)
+ (UIColor)myCustomColor
{
return [UIColor colorWithRed:4/255.0 green:181/255.0 blue:13/255.0 alpha:1.0];
}
@end
In the class that needs the custom colours:
#import "UIColor+Branding.h"
lblTitle.textColor = [UIColor myCustomColor];
This method allows for more flexibility with the branding in your application. However, if you don't care for that, then in your prefix, just import the UIColor+Branding header file and use it everywhere, or use the other methods.
I use a structure like this but with swift.
class Stuff: NSObject {
struct GlobalVariables{
static let ThemeColor = UIColor(red: 41/255, green: 156/255, blue: 253/255, alpha: 1.0)
static let ThemeSecondColor = UIColor.whiteColor()
static let ThemeGrayLineColor = UIColor(red: 128/255, green: 128/255, blue: 128/255, alpha: 1.0)
static let ThemeMyCommentsColor = UIColor(red: 219/255, green: 238/255, blue: 255/255, alpha: 1.0)
static let ThemeCommentedToMeColor = UIColor(red: 227/255, green: 232/255, blue: 235/255, alpha: 1.0)
static let ThemeFontName = "HelveticaNeue-Light"
static let ThemeFontNameBold = "Helvetica-Bold"
static let DeviceId = UIDevice.currentDevice().identifierForVendor.UUIDString
static let MachineName = DeviceInfo.DeviceName()
static let MachineProcessedName = Coz().MachineProcessName()
static let SystemVersion = UIDevice.currentDevice().systemVersion
static let DeviceLanguage = NSBundle.mainBundle().preferredLocalizations[0] as NSString
static let ScreenWidth = UIScreen.mainScreen().bounds.size.width
static let ScreenHeight = UIScreen.mainScreen().bounds.size.height
}
}
I access my colors like this:
self.view.backgroundColor = Coz.GlobalVariables.ThemeSecondColor
So this is basically my custom hardcoded css for iOS.