我想在Utility文件中创建一个常用方法,为我的应用程序中的标签设置颜色。通过应用,我对所有标签都有不同的颜色。
如何编写常用方法来设置颜色?。
答案 0 :(得分:2)
我建议使用一个类别。制作一个名为“UIColor + Branding”的类别或类似的东西。在这里,您将获得所需颜色的列表。您可以执行的操作只是将该类别导入到需要这些自定义颜色import "UIColor+Branding.h"
的文件中,然后您就可以调用[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
在需要自定义颜色的类中:
#import "UIColor+Branding.h"
lblTitle.textColor = [UIColor myCustomColor];
此方法可让您在应用程序中更加灵活地使用品牌。但是,如果你不关心它,那么在你的前缀中,只需导入UIColor + Branding头文件并在任何地方使用它,或者使用其他方法。
答案 1 :(得分:-2)
我使用像这样的结构,但是使用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
}
}
我可以像这样访问我的颜色:
self.view.backgroundColor = Coz.GlobalVariables.ThemeSecondColor
所以这基本上是我的iOS自定义硬编码css。