0

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?.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rakesh
  • 1,177
  • 1
  • 15
  • 31
  • use category for that – Injectios Jan 16 '15 at 09:29
  • if you want change background color of Label then use thsi => lableObject.backgroundColor or if you want change color od text then use this method => lableObject.textColor you can assign both method common value [UIColor yourColor] .. – Tirth Jan 16 '15 at 09:30

2 Answers2

2

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Joey Clover
  • 756
  • 5
  • 14
  • Appreciate your code. – Ashish Kakkad Jan 16 '15 at 10:23
  • I didn't hate your answer, I just don't agree with the method proposed. I didn't say it was bad, either. Yours is better for Applications that don't require branding and this solution is better for Branding multiple applications (white-label apps, etc). Sorry if it seemed like I was insulting your answer. – Joey Clover Jan 16 '15 at 10:25
  • I've been on a project where the method used was like Ashish's answer (constants file) and like this answer (UIColor category), and I prefer the category option. My reasoning is that it leads to more natural code, especially if the created class method matches a your naming convention, so it'd look like '[UIColor tcToolbarTintColor]`. In the category's implementation file, I also like to make constants for the main colors I'll be using, so instead of changing values in your categories code, the rgb values are all at the top of the .m file for you. – timgcarlson Jan 16 '15 at 16:15
-2

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.

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168