有人可以帮我解决这个警告吗?
' 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;
}
答案 0 :(得分:2)
这对我有用
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]];
}
答案 1 :(得分:1)
尝试使用此
+ (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);
答案 2 :(得分:0)
新的sizeWithAttributes
与NSParagraphStyle
相结合,应该为您完成工作。
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];