应用程序在更改字体时崩溃

时间:2013-04-19 07:44:53

标签: ios uifont

这是我改变导航栏背景并尝试设置这样的字体

的方法
UIImage *image = [UIImage imageNamed:@"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];        

UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 40)];
[tmpTitleLabel font:[UIFont systemFontOfSize:12]];  
// APP CRASH (IF I ERASH THIS ABOVE LINE THEN TITLE GET DISPLAYED AS FACEBOOK )

tmpTitleLabel.text = @"Facebook";
tmpTitleLabel.backgroundColor = [UIColor clearColor];

tmpTitleLabel.textColor = [UIColor whiteColor];

CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];

[self.navigationController.navigationBar addSubview:newView];

我做错了什么?我检查了很多答案,但这就是他们设置字体的方式。

2 个答案:

答案 0 :(得分:3)

[tmpTitleLabel font:[UIFont systemFontOfSize:12]];

这是错误的。你使用的是getter而不是setter。您错过了set部分和首都F

[tmpTitleLabel setFont:[UIFont systemFontOfSize:12]];
               ^^^^ 

或者像这样使用点语法:

tmpTitleLabel.font = [UIFont systemFontOfSize:12];

答案 1 :(得分:2)

指定如下字体:

 tmpTitleLabel.font=[UIFont systemFontOfSize:12];

或使用setFont:method

 [tmpTitleLabel setFont:[UIFont systemFontOfSize:12]];