为了让下面的代码工作,我们通常应该用aFont在TestView.h中定义一个UI_APPEARANCE_SELECTOR属性。但是,这似乎不是必需的。我尝试使用和不使用UI_APPEARANCE_SELECTOR,结果是一样的。
我不知道这是对还是错?
[TestView外观设置: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.
}
@endTestView.h
#import <UIKit/UIKit.h>
@interface TestView : UIView
@property (strong,nonatomic) UIFont *aFont ;
@endTestView.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;
}发布于 2015-06-27 18:24:52
您不需要使用UIAppearance协议,除非您想要控制未编码的视图的子视图,或者希望控制外观系统。这里不需要。直接设置字体。
在TestView.m中,可以这样做:
- (void)setAFont:(UIFont *)font
{
_aFont = font;
self.aTestLabel.font = _aFont;
}https://stackoverflow.com/questions/31090039
复制相似问题