¿Cómo puedo cambiar la fuente de todas las etiquetas en un comando?

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 like more than 20 labels in one view and all with different names like total score name.

is there a shorter way to change the font for all of them to the same font type ?

preguntado el 09 de marzo de 12 a las 13:03

5 Respuestas

do fast enumeration..

 for( UIView *view in self.subviews)
    {
    if([view isKindOfClass:[UILabel Class]])
    {
    [(UILabel *)view setFont:[UIFont fontWithName:@"Times New Roman" size:22]];

    }
    }

respondido 09 mar '12, 13:03

can you please explain more how to do it ? i`m new to this stuff - alfarsi

copy this code to your code(wherever you want to change the font.. basically you are checking each view in your view.. if it is type of UIlabel so changing its font.. this code will change all labels font.. - Shubhank

thanks, it worked , but i needed to change self.subviews to self.view.subviews and UILabel Class to UILabel class - alfarsi

For 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]];
        }
    }

respondido 09 mar '12, 14:03

This effect can be achieved also through UILabel category using technique of Method Swizzling: http://darkdust.net/writings/objective-c/method-swizzling#Step_2:_Create_the_wrapper_method

Encabezamiento:

#import <UIKit/UIKit.h>

@interface UILabel (Swizzling)

- (UIFont *)swizzledFont;

@end

Implementación:

#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

respondido 10 mar '13, 21:03

en .h

UIFont *myCustomFont;

En m

myCustomFont = [UIFont fontWithName:@"Times New Roman" size:22];

[Name setFont:myCustomFont];//Name is your UILabel//

Or right in prifix.pch file to get through out the project.

Respondido el 20 de junio de 20 a las 10:06

this code is the same of mine , i`ll have to set the font for each label - alfarsi

Anular UIFont como tal:

Crean UIFont+SytemFontOverride.h:

#import <UIKit/UIKit.h>

@interface UIFont (SytemFontOverride)
@end

y 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 font names accordingly.

respondido 16 nov., 15:00

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.