UI_APPEARANCE_SELECTOR 是否需要?

UI_APPEARANCE_SELECTOR required or not?

为了使下面的代码正常工作,我们通常应该在 TestView.h 和 UI_APPEARANCE_SELECTOR 中定义 aFont 属性。但是,这似乎不是必需的。我尝试使用和不使用 UI_APPEARANCE_SELECTOR,结果都是一样的。

不知道这样说对不对?

[[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;    
}

您不需要使用 UIAppearance 协议,除非您想要控制您没有自己编写代码的视图的子视图,或者您想要控制整个系统的外观。这里不需要。直接设置字体就可以了

在 TestView.m 中做这样的事情:

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