oscarimonbox
3/16/2017 - 8:42 AM

Cambio de fuente de view (tamaño o tipo)

Cambio de fuente de view (tamaño o tipo)

//Crear una categoría de UIView (ChangeFont)
//El tamaño de la fuente se puede subir 2 o 4 puntos y queda bien

1) UIView+ChangeFont.h
//
//  UIView+ChangeFont.h
//

#import <UIKit/UIKit.h>

#define FONT @"Montserrat-Regular"
#define FONT_BOLD @"Montserrat-Bold"
#define FONT_LIGHT @"Montserrat-Regular"

@interface UIView (ChangeFont)

@end


2) UIView+ChangeFont.m
//
//  UIView+ChangeFont.m
//

#import "UIView+ChangeFont.h"

@implementation UIView (ChangeFont)

- (void) awakeFromNib {
    [super awakeFromNib];
    UIFont *newFont;
    
    if([self respondsToSelector:@selector(font)]){
        
        UIFont *font = [((CATextLayer*)self) font];
        int size = font.pointSize;
        
        if (iPad) {
            size = size + increasePointForiPad;
        }
        
        if ([font.fontName rangeOfString:@"Bold"].location!=NSNotFound) {
            newFont = [UIFont fontWithName:FONT_BOLD size:size];
        }
        else if ([font.fontName rangeOfString:@"Light"].location!=NSNotFound) {
            newFont = [UIFont fontWithName:FONT_LIGHT size:size];
        }
        else {
            newFont = [UIFont fontWithName:FONT size:size];
        }
        
        [((CATextLayer*)self) setFont:(__bridge CFTypeRef _Nullable)(newFont)];
    }
    
}
@end