根据文字计算label的宽度和高度

时间:2022-05-06 23:20:32

// 滚动条中的描述文字

        UILabel *desLabel = (UILabel *)[appCell.mAppDescription viewWithTag:101];

   desLabel.numberOfLine = 0;

        desLabel.text = _appDetailModel.mDescription;

        NSLog(@"desLabel.text = %@", desLabel.text);

        

        UIFont *font = [UIFont fontWithName:@"Arial" size:10];//跟label的字体大小一样

        CGSize size = CGSizeMake(300, 29999);//跟label的宽设置一样

        

        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];

        size =[desLabel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:dic context:nil].size;

        appCell.mAppDescription.contentSize = size;

 

************************************************************************************************************************

// 徽章不隐藏

        self.hidden = NO;

        // 设置提醒文字

        [self setTitle:badgeValue forState:UIControlStateNormal];

        // 设置frame

        CGRect frame = self.frame;

        CGFloat BadgeH = self.currentBackgroundImage.size.height;

        CGFloat badgeW = self.currentBackgroundImage.size.width;

        // 消息数 > 9(超过一位数)

        if (badgeValue.length > 1)

        {

            CGSize badgeSize = [badgeValue sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Arial" size:11]}];

//            CGSize badgeSize = [badgeValue sizeWithFont:self.titleLabel.font];

            badgeW = badgeSize.width + 10;

        }

        frame.size.width = badgeW;

        frame.size.height = BadgeH;