Adjust UILabel height depending on the text

前端 未结 30 2245
走了就别回头了
走了就别回头了 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:22

    NSString *str = @"Please enter your text......";
    CGSize lblSize = [str sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize: CGSizeMake(200.0f, 600.0f) lineBreakMode: NSLineBreakByWordWrapping];
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(60, 20, 200, lblSize.height);
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = [UIFont systemFontOfSize:15];
    label.text = str;
    label.backgroundColor = [UIColor clearColor];
    [label sizeToFit];
    [self.view addSubview:label];
    

提交回复
热议问题