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?
make a quick listing.
for( UIView *view in self.subviews)
{
if([view isKindOfClass:[UILabel Class]])
{
[(UILabel *)view setFont:[UIFont fontWithName:@"Times New Roman" size:22]];
}
}
For a specific label;
for( UIView *v in self.view.subviews) {
if([v isKindOfClass:[UILabel Class]]) {
if (v.tag == 1453)
[(UILabel *)v setFont:[UIFont fontWithName:@"Times New Roman" size:22]];
}
}
in .h
UIFont * myCustomFont;
in .m
myCustomFont = [UIFont fontWithName: @ "Times New Roman" size: 22];
[Name setFont: myCustomFont]; // Name is your UILabel //
Or directly in the prifix.pch file to execute the project.
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
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.