简体   繁体   English

UILabel 字体大小?

[英]UILabel font size?

I can't seem to modify the font size of a UILabel with the following code:我似乎无法使用以下代码修改 UILabel 的字体大小:

itemTitle.font = [UIFont systemFontOfSize:25];

As i increase the number 25 to something greater, it seems to only add a top margin to the label, which consequently pushes the text down so much, so that the text gets cut off at the bottom or completely overflows.当我将数字 25 增加到更大的值时,它似乎只为标签添加了一个上边距,因此将文本向下推了很多,因此文本在底部被截断或完全溢出。

i have another UILabel elsewhere with systemFontOfSize 25, and it's much smaller than the itemTitle text.我在其他地方有另一个 UILabel,systemFontOfSize 为 25,它比 itemTitle 文本小得多。 What's going on?这是怎么回事? Isn't 25 supposed to be an absolute value? 25 不应该是一个绝对值吗?

i am so confused on how to programmatically change font size of uilabels.我对如何以编程方式更改 uilabels 的字体大小感到困惑。

I have modified the UILabel by following code:我通过以下代码修改了 UILabel:

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

Try this and see whether is it working in your case or not???试试这个,看看它是否适用于你的情况???

Check that your labels aren't set to automatically resize.检查您的标签是否设置为自动调整大小。 In IB, it's called "Autoshrink" and is right beside the font setting.在 IB 中,它被称为“Autoshrink”,就在字体设置旁边。 Programmatically, it's called adjustsFontSizeToFitWidth .以编程方式,它被称为adjustsFontSizeToFitWidth

[label setFont:[UIFont systemFontOfSize:9]];

这对我有用。

For Swift 3.1 , Swift 4 and Swift 5 , if you only want change the font size for a label :对于Swift 3.1Swift 4Swift 5 ,如果您只想更改标签的字体大小:

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

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

This worked for me in这对我有用

Swift 3斯威夫特 3

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

Swift 4斯威夫特 4

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

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.这是 Swift 版本。

@IBOutlet weak var priceLabel: UILabel!

*.... lines of code later*

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

In C# These ways you can Solve the problem, In UIkit these methods are available.在 C# 中这些方法可以解决问题,在 UIkit 中这些方法可用。

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

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

 [label setframe:CGRect(x,y,widht,height)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM