89

我似乎无法使用以下代码修改 UILabel 的字体大小:

itemTitle.font = [UIFont systemFontOfSize:25];

当我将数字 25 增加到更大的值时,它似乎只在标签上添加了一个上边距,从而将文本向下推了很多,以至于文本在底部被截断或完全溢出。

我在其他地方有另一个 UILabel systemFontOfSize 25,它比 itemTitle 文本小得多。这是怎么回事?25不应该是一个绝对值吗?

我对如何以编程方式更改 uilabels 的字体大小感到非常困惑。

4

11 回答 11

180

我通过以下代码修改了 UILabel:

label.font=[label.font fontWithSize:25];

试试这个,看看它是否适用于你的情况???

于 2012-05-07T06:07:24.127 回答
56

检查您的标签是否未设置为自动调整大小。在 IB 中,它被称为“Autoshrink”,位于字体设置旁边。以编程方式,它被称为adjustsFontSizeToFitWidth.

于 2012-05-07T01:00:33.650 回答
35
[label setFont:[UIFont systemFontOfSize:9]];

这对我有用。

于 2013-03-18T09:59:38.093 回答
21

对于Swift 3.1Swift 4Swift 5,如果您只想更改标签的字体大小:

let myLabel : UILabel = ...
myLabel.font = myLabel.font.withSize(25)
于 2017-04-12T14:23:42.447 回答
12

**您可以通过这些属性设置字体大小**

timedisplayLabel= [[UILabel alloc]initWithFrame:CGRectMake(70, 194, 180, 60)];

[timedisplayLabel setTextAlignment:NSTextAlignmentLeft];

[timedisplayLabel setBackgroundColor:[UIColor clearColor]];

[timedisplayLabel setAdjustsFontSizeToFitWidth:YES];

[timedisplayLabel setTextColor:[UIColor blackColor]];

[timedisplayLabel setUserInteractionEnabled:NO];

[timedisplayLabel setFont:[UIFont fontWithName:@"digital-7" size:60]];

timedisplayLabel.layer.shadowColor =[[UIColor whiteColor ]CGColor ];

timedisplayLabel.layer.shadowOffset=(CGSizeMake(0, 0));

timedisplayLabel.layer.shadowOpacity=1;

timedisplayLabel.layer.shadowRadius=3.0;

timedisplayLabel.layer.masksToBounds=NO;

timedisplayLabel.shadowColor=[UIColor darkGrayColor];

timedisplayLabel.shadowOffset=CGSizeMake(0, 2);
于 2013-07-31T14:25:40.130 回答
6

这对我有用

斯威夫特 3

label.font = label.font.fontWithSize(40.0)

斯威夫特 4

label.font = label.font.withSize(40.0)
于 2017-03-04T05:48:13.423 回答
5

以编程方式调整标签文本大小的非常简单但有效的方法:-

label.font=[UIFont fontWithName:@"Chalkduster" size:36];

:-)

于 2013-12-10T11:39:35.640 回答
1

这对我有用:

sequencerPlayLabel.font = [UIFont fontWithName:kTypeFont size:kTypeFontSize];

-富有的

于 2014-04-17T14:48:39.770 回答
1

上面的答案有很大帮助。

这是斯威夫特版本。

@IBOutlet weak var priceLabel: UILabel!

*.... lines of code later*

self.priceLabel.font = self.priceLabel.font.fontWithSize(22)
于 2015-05-12T13:40:12.150 回答
1

在 C# 中这些方法可以解决问题,在 UIkit 中这些方法是可用的。

Label.Font = Label.Font.WithSize(5.0f);
       Or
Label.Font = UIFont.FromName("Copperplate", 10.0f);  
       Or
Label.Font = UIFont.WithSize(5.0f);
于 2019-01-05T06:26:15.110 回答
-1

尝试更改标签框架大小的高度和宽度,以免文本被剪切。

 [label setframe:CGRect(x,y,widht,height)];
于 2012-05-07T06:16:47.897 回答