UILabel 字体大小?

我似乎不能用下面的代码修改 UILabel 的字体大小:

itemTitle.font = [UIFont systemFontOfSize:25];

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

我还有另外一个 UILabel,它是 systemFontOfSize 25,比 itemTitle 文本小得多。发生什么事了?25不是一个绝对值吗?

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

125620 次浏览

Check that your labels aren't set to automatically resize. In IB, it's called "Autoshrink" and is right beside the font setting. Programmatically, it's called adjustsFontSizeToFitWidth.

I have modified the UILabel by following code:

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

Try this and see whether is it working in your case or not???

Try to change your label frame size height and width so your text not cut.

 [label setframe:CGRect(x,y,widht,height)];
[label setFont:[UIFont systemFontOfSize:9]];

this works for me.

**You can set font size by these properties **

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);

very simple, yet effective method to adjust the size of label text progmatically :-

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

:-)

This worked for me:

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

-rich

Answers above helped greatly.

Here is the Swift version.

@IBOutlet weak var priceLabel: UILabel!


*.... lines of code later*


self.priceLabel.font = self.priceLabel.font.fontWithSize(22)

This worked for me in

Swift 3

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

Swift 4

label.font = label.font.withSize(40.0)

For Swift 3.1, Swift 4 and Swift 5, if you only want change the font size for a label :

let myLabel : UILabel = ...
myLabel.font = myLabel.font.withSize(25)

In C# These ways you can Solve the problem, In UIkit these methods are available.

Label.Font = Label.Font.WithSize(5.0f);
Or
Label.Font = UIFont.FromName("Copperplate", 10.0f);
Or
Label.Font = UIFont.WithSize(5.0f);