UILabel子类 [英] UILabel subclass

查看:112
本文介绍了UILabel子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个新手问题,但我是一个新手,所以这里:

I know that this is a newbie question but I am a newbie so here goes:

我希望在我的应用程序中使用Chalkduster字体很多标签等),并试图子类化UILabel来实现这一点。我在Default.h中有以下:

I wish to use Chalkduster font quite a lot throughout my app (buttons, labels etc) and have tried subclassing UILabel to achieve this. I have the following in Default.h:

#import <UIKit/UIKit.h>

@interface Default : UILabel
{
UILabel *theLabel;
}

@property (nonatomic, strong) IBOutlet UILabel *theLabel;

@end



and this in my .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

界面构建器和运行,我没有看到Chalkduster字体。我会感谢一个手,得到这个设置,因为我相信它会救我很多时间。
Cheers。

When I change the class in interface builder and run, I'm not seeing the Chalkduster font. I'd appreciate a hand in getting this set up as I believe it will save me a lot of time. Cheers.

推荐答案

需要解决的一些问题:

1)你正在混淆默认 正在标签和默认 包含标签。要子类化,删除你的类中的属性,并改变 self ,而不是 theLabel code> if(self){节)。

1) You're mixing up the idea of Default being a label and Default containing a label. To subclass, get rid of the property inside your class and make your changes to self rather than theLabel (inside the if (self) { section).

2)无条件 不会被执行...我惊讶的是,编译器没有抱怨这些语句。

2) Anything you code after an unconditional return isn't going to get executed...and I'm surprised the compiler didn't complain about those statements.

编辑:。

3)如果从xib或storyboard加载,初始化由 initWithCoder:而不是 initWithFrame:,所以:

3) If you're loading from a xib or storyboard, the initialization is done by initWithCoder: instead of initWithFrame:, so:

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.font = [UIFont fontWithName:@"Chalkduster" size:18];
    }
    return self;
}

这篇关于UILabel子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆