の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];
@usage:色で:
ここ
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];
+1ただし、あなたのアプリがUITableViewを使用している場合、この方法は失敗します。個々のセル内のラベルは、このプロセスによって更新されません。 – Fostah
はい、でも何ですか? UITableViewCellのUITableViewCellsでの処理は、テーブルのcellForRowAtIndexPathメソッドの1行のテキストの問題です。 –
これは可能な限り最善の方法かもしれませんが、このソリューションでは扱えないもう一つの問題は、 "FagoOfficeSans-Bold"などのフォント用に別のファミリがある場合、太字や斜体などの異なるフォントスタイルです。 – d512