开发者

New label class in iPhone with custom font

开发者 https://www.devze.com 2023-01-30 14:15 出处:网络
As it seems that it is not possible to apply a custom font through Interface Builder, I tried to define a new UILabel-derived class and set in its - (id)init method the custom f开发者_StackOverflowont

As it seems that it is not possible to apply a custom font through Interface Builder, I tried to define a new UILabel-derived class and set in its - (id)init method the custom f开发者_StackOverflowont I'd like to use: this doesn't lead to the expected result as the font used at runtime seems to be still the 'Helvetica' (my custom font is 'african' and it works if set via code).

Here is my UILabel-derived snippet:

- (id)init {
    UIFont *font = [UIFont fontWithName:@"african" size:10];
    [self setFont:font];    

    return self;
}

Therefore I'm currently solving the problem by forcing the font via code:

NSLog(@"Before FONT NAME IS ------------> %@", myLabel.font.fontName);  
UIFont *font = [UIFont fontWithName:@"african" size:10];
[myLabel setFont:font];
myLabel.text = @"HELLO!";
NSLog(@"After FONT NAME IS ------------> %@", myLabel.font.fontName);

The printout is the following:

Before FONT NAME IS ------------> Helvetica
After FONT NAME IS ------------> African

It seems that the default font I set in the init method is overwritten: where else should I set it?


If you use your init function like that "self" will have no value… All init functions have to look like this

- (id)init {
    self = [super init];
    if (self) {
        // Your init code here
    }
    return self;
}

Additionally make sure you'll override "initWithFrame:" too, and perhaps "initWithCoder:" if you want to use it from IB.

0

精彩评论

暂无评论...
验证码 换一张
取 消