Adjust UILabel height depending on the text

前端 未结 30 1803
走了就别回头了
走了就别回头了 2020-11-22 03:53

Consider I have the following text in a UILabel (a long line of dynamic text):

Since the alien army vastly outnumbers the team, players m

30条回答
  •  臣服心动
    2020-11-22 04:10

    Thanks guys for help, here is the code I tried which is working for me

       UILabel *instructions = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 300, 180)];
       NSString *text = @"First take clear picture and then try to zoom in to fit the ";
       instructions.text = text;
       instructions.textAlignment = UITextAlignmentCenter;
       instructions.lineBreakMode = NSLineBreakByWordWrapping;
       [instructions setTextColor:[UIColor grayColor]];
    
       CGSize expectedLabelSize = [text sizeWithFont:instructions.font 
                                    constrainedToSize:instructions.frame.size
                                        lineBreakMode:UILineBreakModeWordWrap];
    
        CGRect newFrame = instructions.frame;
        newFrame.size.height = expectedLabelSize.height;
        instructions.frame = newFrame;
        instructions.numberOfLines = 0;
        [instructions sizeToFit];
        [self addSubview:instructions];
    

提交回复
热议问题