'sizeWithFont:constrainedToSize:lineBreakMode:' 已弃用:首先在 iOS 7.0 中弃用 - 使用 -boundingRectWithSize:options:attributes:context:

'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithSize:options:attributes:context:

谁能帮我解决这个警告?

'sizeWithFont:constrainedToSize:lineBreakMode:' 已弃用:首先在 iOS 7.0 中弃用 - 使用 -boundingRectWithSize:options:attributes:context:

-(CGFloat)setLableSizeAccordingToText:(NSString*)text andSetX:(CGFloat)x Y:(CGFloat)y{

    self.text = text;


    CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

     CGSize expectedLabelSize = [text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:self.lineBreakMode];

    CGRect frame = CGRectMake(x, y, expectedLabelSize.width+lblHorizontalPadding , lblHeight);

    self.frame = frame;

    return expectedLabelSize.width + lblHorizontalPadding;
}

新的 sizeWithAttributesNSParagraphStyle 相结合应该可以为您完成这项工作。

    NSMutableDictionary *attributes = [NSMutableDictionary new];
    [attributes setObject:self.font forKey:NSFontAttributeName];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = self.lineBreakMode;
    //paragraphStyle.alignment = self.textAlignment; //uncomment this if you need specific text alignment
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    CGSize expectedLabelSize = [text sizeWithAttributes:attributes];

试试这个

+ (CGSize)DescriptionHeight:(NSString *)str{

   CGSize detailSize =   [str boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{
                                                          NSFontAttributeName:[UIFont fontWithName:@"Cronos Pro" size:14.0f]
                                                          }
                                                context:nil].size;
    return detailSize;

}

或者你也可以这样使用

CGSize stringsize = [Your_Str_Value sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.0f]}];
YourBtn.frame = CGRectMake(10, 5, stringsize.width, 44);

对我有用

UILabel *myLabel;
CGSize textSize;
if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
    textSize = [myLabel.text sizeWithFont:[myLabel font]];
}else{
    textSize = [myLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObject:[myLabel font] forKey:NSFontAttributeName]];
}