繁体   English   中英

'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;
}

它为我工作

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]];
}

试试这个

+ (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);

NSParagraphStyle结合使用的新的sizeWithAttributes应该可以为您完成这项工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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