是否需要UI_APPEARANCE_SELECTOR?

时间:2015-06-27 14:55:45

标签: ios ios8 xcode6

对于下面的代码,我们通常应该在TestView.h中使用UI_APPEARANCE_SELECTOR定义一个aFont属性。但是,它似乎并不是必需的。我尝试使用和不使用UI_APPEARANCE_SELECTOR,结果是相同的。

我不知道这是对还是错?

  

[[TestView外观] 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;    
}

1 个答案:

答案 0 :(得分:0)

您不需要使用UIAppearance协议,除非您想要控制您自己未编码的视图的子视图,或者您想要控制系统外观。这里不需要它。只需直接设置字体即可。

在TestView.m中执行以下操作:

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