最近项目中碰到UI给出的切图中,大部分Label的文本颜色都是 16进制颜色#23b6bc,但是UILabel 控件默认好像都是黑色还是什么颜色来着。当时是想到了两种解决方案。
1.自定义一个UILabel子类,在子类中设置label的通用样式,以后就用这个子类去实例化使用即可。
2.用runtime 可以替换类的init方法,再替换的init方法中设置一下空间的默认样式。这种方法感觉实用性更好一些。。。
(1)新建一个UILabel的分类,这里我命名UILabel+DefaultTextColor
(2)建好分类后,在分类的.m文件中添加替换的方法
/**
*在这些方法中将你的字体名字换进去
*/
- (instancetype)ZpfDefInit
{
id__self = [selfZpfDefInit];
UIColor * textColor=GetJinZhiColor(@"#444444");
self.textColor=textColor;
return__self;
}
-(instancetype)ZpfDefBaseInitWithFrame:(CGRect)rect{
id__self = [selfZpfDefBaseInitWithFrame:rect];
UIColor * textColor=GetJinZhiColor(@"#444444");
self.textColor=textColor;
return__self;
}
-(void)ZpfDefBaseAwakeFromNib{
[selfZpfDefBaseAwakeFromNib];
UIColor * textColor=GetJinZhiColor(@"#444444");
self.textColor=textColor;
}
这三个方法分别用来替换init , initWithFrame , xib初始化方法。这样使用的时候无论用哪个方法初始化都能够设置自定义的样式。
(3)最重要的方法,复写类方法+(void)load ,这个方法是当类被加载时候调用的,在这个方法里进行需要的方法替换即可。
+(void)load{
//只执行一次这个方法
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [selfclass];
// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);
//替换三个方法
SELoriginalSelector =@selector(init);
SELoriginalSelector2 =@selector(initWithFrame:);
SELoriginalSelector3 =@selector(awakeFromNib);
SELswizzledSelector =@selector(ZpfDefInit);
SELswizzledSelector2 =@selector(ZpfDefBaseInitWithFrame:);
SELswizzledSelector3 =@selector(ZpfDefBaseAwakeFromNib);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);
BOOLdidAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
BOOLdidAddMethod2 =
class_addMethod(class,
originalSelector2,
method_getImplementation(swizzledMethod2),
method_getTypeEncoding(swizzledMethod2));
BOOLdidAddMethod3 =
class_addMethod(class,
originalSelector3,
method_getImplementation(swizzledMethod3),
method_getTypeEncoding(swizzledMethod3));
if(didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
if(didAddMethod2) {
class_replaceMethod(class,
swizzledSelector2,
method_getImplementation(originalMethod2),
method_getTypeEncoding(originalMethod2));
}else{
method_exchangeImplementations(originalMethod2, swizzledMethod2);
}
if(didAddMethod3) {
class_replaceMethod(class,
swizzledSelector3,
method_getImplementation(originalMethod3),
method_getTypeEncoding(originalMethod3));
}else{
method_exchangeImplementations(originalMethod3, swizzledMethod3);
}
});
}
(4)完成以上三步后,在appdelegate.m中引用分类的头文件 #import"UILabel+DefaultTextColor.h"
替换完以后,就能一劳永逸的设置通用的label样式,而不需每次都调用label.textcolor方法去设置文本颜色,同理所有UIKit的控件,或是需要设置通用属性的时候用这个方法就能达到效果
网友评论