简体   繁体   中英

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.

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. In portrait mode, the screen has enough space in heightand don't scale my UILabel in height to a critical point.

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.

I have tested everyting i have read like:

  • set line breaks to clip mode

  • use 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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