通过将 UILabel 子类化以在故事板上使用的标准文本颜色 - (IBOutlet)
Standard text colour by subclassing UILabel for use on a storyboard - (IBOutlet)
我希望在我的应用程序中创建标准外观,其中 some UILabel
具有特定的 textColor
.
我的 UIViewController
上有几个 UILabel
,在 viewDidLoad
方法中我调用了一个设置 textColor
属性 的函数:
[[CHToolKit sharedToolkit] uiLabelAppearanceFeatured:[self IBOLabelStepsRemaining]];
CHToolkit
是我为杂项任务创建的单例。
而且,上面的效果很好。但我必须在任何需要自定义颜色的 UILabel
的地方执行此操作。现在,我想到一个更谨慎的做法是将 UILabel
子 class(我称之为 CHLabel
)创建一个我自己的实例,然后在它的 init 方法,在那里定义 textColor
。然后,在向我的 VC 添加新的 UILabel 时,我可以选择我的自定义 class 而不是 UILabel
.
我自定义的实现UILabel
简单如下:
#import "CHToolKit.h"
#import "CHLabel.h"
@implementation CHLabel
- (CHLabel *)init {
if (self = [super init]) {
[self setTextColor:kColourDefaultAppColourText];
}
return self;
} // init
@end
因此,当我将 UILabel
拖到我的故事板上(在 Interface Builder 中)时,我然后从自定义 class 部分选择 CHLabel
,(替换 UILabel
) .而这,它让我可以做到。
现在,当 运行 时,我希望我的 CHLabel
有不同的颜色,即在我的自定义 class 中定义的颜色。但它不会。它保持黑色,或在 IB 中设置的任何颜色。
知道我做错了什么吗?
正确的方法是使用外观代理。
要更改所有 UILabel 的文本颜色,您可以使用
[[UILabel appearance] setTextColor: kColourDefaultAppColourText];
在
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
您也可以使用
+ (instancetype)appearanceWhenContainedIn:(Class<UIAppearanceContainer>)ContainerClass
更改包含在特定视图控制器中的 UILabel 的外观。
那是因为在 init 方法中没有 UI 个对象还没有被初始化。您需要在 awakeFromNib 方法中进行。
-(void)awakeFromNib {
[super awakeFromNib];
[self setTextColor:kColourDefaultAppColourText];
}
或者你可以使用外观。将其设置为例如。 didFinishLaunchingWithOptions 方法与您的子类:
[[CHLabel appearance] setTextColor:kColourDefaultAppColourText];
我希望在我的应用程序中创建标准外观,其中 some UILabel
具有特定的 textColor
.
我的 UIViewController
上有几个 UILabel
,在 viewDidLoad
方法中我调用了一个设置 textColor
属性 的函数:
[[CHToolKit sharedToolkit] uiLabelAppearanceFeatured:[self IBOLabelStepsRemaining]];
CHToolkit
是我为杂项任务创建的单例。
而且,上面的效果很好。但我必须在任何需要自定义颜色的 UILabel
的地方执行此操作。现在,我想到一个更谨慎的做法是将 UILabel
子 class(我称之为 CHLabel
)创建一个我自己的实例,然后在它的 init 方法,在那里定义 textColor
。然后,在向我的 VC 添加新的 UILabel 时,我可以选择我的自定义 class 而不是 UILabel
.
我自定义的实现UILabel
简单如下:
#import "CHToolKit.h"
#import "CHLabel.h"
@implementation CHLabel
- (CHLabel *)init {
if (self = [super init]) {
[self setTextColor:kColourDefaultAppColourText];
}
return self;
} // init
@end
因此,当我将 UILabel
拖到我的故事板上(在 Interface Builder 中)时,我然后从自定义 class 部分选择 CHLabel
,(替换 UILabel
) .而这,它让我可以做到。
现在,当 运行 时,我希望我的 CHLabel
有不同的颜色,即在我的自定义 class 中定义的颜色。但它不会。它保持黑色,或在 IB 中设置的任何颜色。
知道我做错了什么吗?
正确的方法是使用外观代理。
要更改所有 UILabel 的文本颜色,您可以使用
[[UILabel appearance] setTextColor: kColourDefaultAppColourText];
在
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
您也可以使用
+ (instancetype)appearanceWhenContainedIn:(Class<UIAppearanceContainer>)ContainerClass
更改包含在特定视图控制器中的 UILabel 的外观。
那是因为在 init 方法中没有 UI 个对象还没有被初始化。您需要在 awakeFromNib 方法中进行。
-(void)awakeFromNib {
[super awakeFromNib];
[self setTextColor:kColourDefaultAppColourText];
}
或者你可以使用外观。将其设置为例如。 didFinishLaunchingWithOptions 方法与您的子类:
[[CHLabel appearance] setTextColor:kColourDefaultAppColourText];