我知道这是一个新手问题,但我是新手,所以这里是:
我希望在我的应用程序(按钮,标签等)中使用Chalkduster字体并且已经尝试了子类化UILabel来实现这一点.我在Default.h中有以下内容:
#import <UIKit/UIKit.h>
@interface Default : UILabel
{
UILabel *theLabel;
}
@property (nonatomic, strong) IBOutlet UILabel *theLabel;
@end
Run Code Online (Sandbox Code Playgroud)
这在我的.m中:
#import "Default.h"
@implementation Default
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIFont *custom = [[UIFont alloc] init];
custom = [UIFont fontWithName:@"Chalkduster" size:18];
self.font = custom;
NSLog(@"h");
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
当我在界面构建器中更改类并运行时,我没有看到Chalkduster字体.我很感激能够帮助我完成这项工作,因为我相信它会为我节省很多时间.干杯.
Phi*_*lls 11
要修复的一些问题:
1)你混淆了Default 作为标签和Default 包含标签的想法.要进行子类化,去除类中的属性并进行更改self而不是theLabel(在if (self) {部分内部).
2)无条件后你编码的任何东西return都不会被执行......我很惊讶编译器没有抱怨这些陈述.
编辑:......还有一件事让我觉得恍然大悟.
3)如果你是从xib或storyboard加载的,那么初始化是由initWithCoder:而不是initWithFrame:,所以:
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.font = [UIFont fontWithName:@"Chalkduster" size:18];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
首先,我不认为你是UILabel正确的子类.所以我为你解释了如何做这个教程.您不需要子类化的IBOutlet对象.只是自己打电话.例如:self.font = ...如果您想要子类UILabel执行此操作:
创建标题为myLabel的新类,如下所示:
.H
#import <UIKit/UIKit.h>
@interface MyLabel : UILabel {
}
@end
Run Code Online (Sandbox Code Playgroud)
.M
#import "MyLabel.h"
@implementation MyLabel
-(void)awakeFromNib {
UIFont *custom = [[UIFont alloc] init];
custom = [UIFont fontWithName:@"Chalkduster" size:18];
self.font = custom;
}
@end
Run Code Online (Sandbox Code Playgroud)
现在在故事板中选择您的标签,然后转到indentity inspector,在Custom Class中选择上面创建的类.像这样:

输出:

注意:不要忘记发布自定义,因为您正在分配它.