0

在我正在从事的一个大型项目中,我遇到了 UILabel 的一些问题,所以我决定尝试在一个新项目中重现它们。我得到的结果动摇了我对 UILabel 如何工作的理解,特别是在参考属性“adjustFontSizeToFitWidth”方面。下面的代码生成图像。

UILabel* label = [[UILabel alloc] init];
label.text = @"TEST";
label.font = [[UIFont alloc] fontWithSize:100.0];
label.adjustsFontSizeToFitWidth = true;
label.frame = CGRectMake(10, 10, 300, 100);
[label.layer setBorderColor:[[UIColor blackColor] CGColor]];
label.layer.borderWidth = 1.0;
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];

使用上述代码创建的 UILabel

所以我的问题是为什么文本字体没有调整为实际适合 UILabel 框架的宽度?

4

2 回答 2

1

[[UIFont alloc] fontWithSize:100.0];不会产生有效的实例,因为您没有指定字体名称,因此标签将使用默认字体。所以它看起来正是它应该做的。尝试正确创建字体并重新运行测试。

于 2013-07-28T23:02:08.257 回答
0

您可以使用 [mylabel setFont:[UIFont fontWithName:@"fontname" size:128.0]];

谢谢

于 2013-07-29T06:55:45.633 回答