How do I create a category in Xcode 6 or higher?

后端 未结 8 1815
太阳男子
太阳男子 2020-11-27 09:53

I want to create a category on UIColor in my app using Xcode 6. But the thing is that in Xcode 6 there is no Objective-C category file template.

Is the

8条回答
  •  爱一瞬间的悲伤
    2020-11-27 10:16

    Extending unmircea's fantastic answer re: how to create a custom category to implement a custom UIColor palette, you could create a category.

    Once you've created your category (in this example, it's a category called ColorPalette of class UIColor), you'll have a header and an implementation file.

    UIColor+ColorPalette.h

    #import 
    
    @interface UIColor (ColorPalette)
    
    // Your custom colors
    
    + (UIColor *) customRedButtonColor;
    + (UIColor *) customGreenButtonColor;
    
    @end
    

    UIColor+ColorPalette.m

    #import "UIColor+ColorPalette.h"
    
    @implementation UIColor (ColorPalette)
    
    // Button Colors
    
    + (UIColor *) customRedButtonColor {
        return [UIColor colorWithRed:178.0/255.0 green:25.0/255.0 blue:0.0/255.0 alpha:1.0];
    }
    
    + (UIColor *) customGreenButtonColor {
        return [UIColor colorWithRed:20.0/255.0 green:158.0/255.0 blue:96.0/255.0 alpha:1.0];
    }
    

    To use your custom color palette, just import the header into the class where you'd like to implement your custom colors:

    #import "UIColor+ColorPalette.h"
    

    and call the color as you would a standard color like redColor, greenColor, or blueColor.

    Here's a link to a slightly more in-depth discussion of creating a custom palette.

    Additionally, here is a tool to help you select the custom color values

提交回复
热议问题