How can I change the font of labels with one command
if i have 3 labels called total, score and name and i want to change the font of the text i use this command
[total setFont:[UIFont fontWithName:@"Times New Roman" size:22]];
[score setFont:[UIFont fontWithName:@"Times New Roman" size:22]];
[name setFont:[UIFont fontWithName:@"Times New Roman" size:22]];
What if I have more than 20 labels in one view and all with different names like the common name of the score.
Is there a shorter way to change the font for all of them to the same font type?
source to share
This effect can be achieved also through the UILabel category using the Swizzling method: http://darkdust.net/writings/objective-c/method-swizzling#Step_2:_Create_the_wrapper_method
Title:
#import <UIKit/UIKit.h>
@interface UILabel (Swizzling)
- (UIFont *)swizzledFont;
@end
Implementation:
#import "UILabel+Swizzling.h"
#import <objc/runtime.h>
@implementation UILabel (Swizzling)
- (UIFont *)swizzledFont
{
return [UIFont fontWithName:@"SourceSansPro-Light" size:[[self swizzledFont] pointSize]];
}
+ (void)load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(font));
swizzled = class_getInstanceMethod(self, @selector(swizzledFont));
method_exchangeImplementations(original, swizzled);
}
@end
source to share
Override UIFont
as such:
Create UIFont+SytemFontOverride.h
:
#import <UIKit/UIKit.h>
@interface UIFont (SytemFontOverride)
@end
and UIFont+SytemFontOverride.m
:
#import "UIFont+SytemFontOverride.h"
@implementation UIFont (SytemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"Bryant-Bold" size:fontSize];
}
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"Bryant-Regular" size:fontSize];
}
#pragma clang diagnostic pop
@end
Change the font names accordingly.
source to share