首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否需要UI_APPEARANCE_SELECTOR?

是否需要UI_APPEARANCE_SELECTOR?
EN

Stack Overflow用户
提问于 2015-06-27 14:55:45
回答 1查看 123关注 0票数 1

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

我不知道这是对还是错?

[TestView外观设置:UIFont boldSystemFontOfSize:20.0f];

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <UIKit/UIKit.h>

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

TestView.m

代码语言:javascript
复制
#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;    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-27 18:24:52

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

在TestView.m中,可以这样做:

代码语言:javascript
复制
- (void)setAFont:(UIFont *)font
{
    _aFont = font;
    self.aTestLabel.font = _aFont;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31090039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档