samedi 27 juin 2015

UI_APPEARANCE_SELECTOR required or not?

For below code to work we should generally define a aFont property in TestView.h with UI_APPEARANCE_SELECTOR. But, it seems it is not required. I tried with and without the UI_APPEARANCE_SELECTOR, results are the same.

I don't know if this is right or wrong?

[[TestView appearance] setAFont:[UIFont boldSystemFontOfSize:20.0f]];

#import "ViewController.h"
#import "TestView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     [[TestView appearance] setAFont:[UIFont boldSystemFontOfSize:20.0f]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

TestView.h

#import <UIKit/UIKit.h>

@interface TestView : UIView
@property (strong,nonatomic) UIFont *aFont  ;
@end

TestView.m

#import "TestView.h"

@implementation TestView
{
    UILabel *aTestLabel;
}
@synthesize aFont=_aFont;

- (void)drawRect:(CGRect)rect {
    // Drawing code
    NSLog(@"A Font= %@ ",_aFont);
}


-(id) init{
    self=[super init];
    if(self){
        [self addLabel];
    }
    return self;
}

-(id) initWithCoder:(NSCoder *)aDecoder{
    self=[super initWithCoder:aDecoder];
    if(self){
        [self addLabel];
    }
    return self;
}

-(void)addLabel{
    aTestLabel=[[UILabel alloc] initWithFrame:self.bounds];
    aTestLabel.text=@"Hello";
    _aFont=[UIFont systemFontOfSize:10.0];

    [self addSubview:aTestLabel];
}

-(void)setAFont:(UIFont *)aFont{
    _aFont=aFont;
    aTestLabel.font=_aFont;    
}

Aucun commentaire:

Enregistrer un commentaire