UIView+DefaultFontAndColor.h
#import <UIKit/UIKit.h>
@interface UIView (DefaultFontAndColor)
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
@end
UIView+DefaultFontAndColor.m
#import "UIView+DefaultFontAndColor.h"
@implementation UIView (DefaultFontAndColor)
//sets the default font for view classes by default
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor: (UIColor*) color
{
if ([self isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)self;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
if( color )
lbl.textColor = color;
}
else if ([self isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)self;
[btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];
if( color )
{
btn.tintColor = color;
}
}
if (isSubViews)
{
for (UIView *sview in self.subviews)
{
[sview setDefaultFontFamily:fontFamily andSubViews:YES andColor:color];
}
}
}
@end
@usage:没有颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:nil];
@用法:颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];