1
對於下面的代碼來說,我們通常應該在TestView.h中用UI_APPEARANCE_SELECTOR定義一個aFont屬性。但是,它似乎並不是必需的。我嘗試了和沒有UI_APPEARANCE_SELECTOR,結果是一樣的。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];
[email protected]"Hello";
_aFont=[UIFont systemFontOfSize:10.0];
[self addSubview:aTestLabel];
}
-(void)setAFont:(UIFont *)aFont{
_aFont=aFont;
aTestLabel.font=_aFont;
}
我控制它全系統的,我在IB兩種觀點,與子類TestView。我在應用程序中設置了TestView外觀:didFinisihLaunching – andyPaul
但是你仍然可以直接設置值,因爲它是你的類。這裏不需要UIA外觀。使用UIAppearance來執行諸如在所有應用程序的UINavigationBars中設置字體的功能,這是一個您無權直接修改的UIKit類。 –
所以,如果子類UINavigationBar(TestNavigationBar.h)並用UI_APPEARANCE_SELECTOR定義了一個新的字體屬性。這種情況與上面解釋的不一樣(這是UIView的一個子類)請問你可以用代碼片段來解釋一下嗎? – andyPaul