简体   繁体   English

UILabel不会调整高度以适合文本

[英]UILabel don't adjust in height to fit the text

I know there is a lot of post about UILabel and it's size but i was unable to find a answer for my problem. 我知道有很多关于UILabel的帖子,它的大小很大,但是我找不到我的问题的答案。

I have some UILabels with a text on 1 line, no more and don't want more lines, and the text is set to a minimum font scale to adjust. 我有一些UILabels的文本在1行上,没有更多行了,也不想增加行数,并且将文本设置为最小字体比例进行调整。 In portrait mode, the screen has enough space in heightand don't scale my UILabel in height to a critical point. 在纵向模式下,屏幕的高度足够大,并且不能将UILabel的高度缩放到临界点。

In landscape mode, i'm short in height and the UILabel is very small in height and the text is cut on the top and bottom. 在横向模式下,我身高不足, UILabel高度非常小,并且在顶部和底部剪切了文本。

I have tested everyting i have read like: 我已经测试了我读过的所有内容:

  • set line breaks to clip mode 将换行符设置为剪辑模式

  • use adjustsFontSizeToFitWidth = YES; 使用adjustsFontSizeToFitWidth = YES;

In every case, the text fit only in width but don't care about height. 在每种情况下,文本仅适合宽度,而不关心高度。

Should i use some ugly hack to calcul the font size depending on the label height myself or have i miss something ? 我应该根据自己的标签高度使用一些难看的技巧来计算字体大小,还是我错过了什么?

CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];

CGFloat fontSize = 0.0f;
labelFrame.size = [myLabel.text sizeWithFont:myLabel.font
                            minFontSize:myLabel.minimumFontSize
                         actualFontSize:&fontSize
                               forWidth:labelFrame.width
                        lineBreakMode:myLabel.lineBreakMode];

myLabel.frame = labelFrame;
[self.view addSubview:myLabel];

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

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