简体   繁体   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:

Can anyone please help me to fix this warning? 谁能帮我解决这个警告?

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

It is working for me 它为我工作

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

Try using this 试试这个

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

}

Or u can use in this way too 或者你也可以这样使用

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

The new sizeWithAttributes combined with NSParagraphStyle should do the job for you. 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.

相关问题 'sizeWithFont(_:constrainedToSize:lineBreakMode :)'不可用:使用-boundingRectWithSize:options:attributes:context: - 'sizeWithFont(_:constrainedToSize:lineBreakMode:)' is unavailable: Use -boundingRectWithSize:options:attributes:context: 将不推荐使用的sizeWithFont:constrainedToSize满足boundingRectWithSize:options:attributes:context: - Contenting deprecated sizeWithFont:constrainedToSize to boundingRectWithSize:options:attributes:context: 不推荐使用sizeWithFont:constrainedToSize:lineBreakMode - sizeWithFont:constrainedToSize:lineBreakMode deprecated 'sizeWithFont:constrainedToSize:lineBreakMode:'已弃用: - 'sizeWithFont:constrainedToSize:lineBreakMode:'is deprecated: sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用 - sizeWithFont: ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7 在iOS 7中替换弃用的-sizeWithFont:constrainedToSize:lineBreakMode: - Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7? 将不推荐使用的sizeWithFont方法修改为boundingRectWithSize:options:attributes:context - Modifying deprecated sizeWithFont method to boundingRectWithSize:options:attributes:context 替换已弃用的“ sizeWithFont:constrainedToSize:lineBreakMode”的正确语法 - Correct syntax for replacement of deprecated “sizeWithFont:constrainedToSize:lineBreakMode” sizeWithFont:constrainedToSize:lineBreakMode:不准确? - sizeWithFont:constrainedToSize:lineBreakMode: not accurate? sizeWithFont:constraintedToSize:已过时,但boundingRectWithSize对我不起作用 - sizeWithFont:constraintedToSize: is deprecated, but boundingRectWithSize not working for me
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM