ios - 更改字体时应用程序崩溃

标签 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];

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

最佳答案

[tmpTitleLabel 字体:[UIFont systemFontOfSize:12]];

这是错误的。您正在使用 getter 而不是 setter。您错过了 set 部分和大写 F

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

或者像这样使用点语法:

tmpTitleLabel.font = [UIFont systemFontOfSize:12];

关于ios - 更改字体时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099761/

相关文章:

ios - 自定义 UIFont 基线偏移

objective-c - 如何安装和使用 iOS 6 中包含的附加亚洲字体?

html - 在具有指定字体的 UILabel 中显示 html 标签

swift - 向 NSMutableAttributedString 添加几个属性

ios - iOS 设备是否可以作为 iBeacon 进行广播并同时监视 iBeacon?

ios - 如何根据不规则形状裁剪图像?

ios - 展开 UITableViewCell

ios - Xcode8.3更新后不断崩溃

ios - GRPCcall2 的用法示例

ios - 无法在 WatchKit 中设置自定义字体