我有一个iPhone应用程序,每年都会有一些代码元素生成不推荐使用的问题。我的应用程序运行得很好,除了一些小的格式问题。我已经尝试使用建议的代码,但它只会导致错误。我真的很想解决这些问题,看看是否能解决格式问题。有人能帮我拿一下这些吗。
First Issue:'sizeWithFont:constrainedToSize:lineBreakMode:‘被弃用: first在iOS 7.0中被弃用-使用-boundingRectWithSize:options:attributes:context:尝试使用建议的替代,但只是导致了错误(参见下面的代码)。不确定在选项、属性和上下文中适合当前代码的位置。
第二个问题:'drawInRect:withFont:lineBreakMode:alignment:‘被弃用:在iOS 7.0中第一次被弃用-使用-drawInRect:withAttributes:尝试使用建议的替代品,但只是导致了错误(参见下面的代码)。不确定在哪里适合当前的代码re withAttributes。
//Draw text fo our header.
CGContextRef currentContextHeader = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContextHeader, 0.3, 0.7, 0.2, 1.0);
NSString *textToDrawHeader = [NSString stringWithFormat:@"%@", enterSubject.text];
UIFont *fontHeader = [UIFont systemFontOfSize:24.0];
//Original Code that generated the issue
//CGSize stringSizeHeader = [textToDrawHeader sizeWithFont:fontHeader constrainedToSize:CGSizeMake(_pageSize.width - 2*kBorderInset-2*kMarginInset, _pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];
//Proposed change that resulted in an error
CGSize stringSizeHeader = [textToDrawHeader boundingRectWithSize:fontHeader options:attributes:context:constrainedToSize:CGSizeMake(_pageSize.width - 2*kBorderInset-2*kMarginInset, _pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];
CGRect renderingRectHeader = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, _pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSizeHeader.height);
int ydistanceToLine = kBorderInset + kMarginInset + stringSizeHeader.height +kMarginInset;
//Original Code that generated the issue
//[textToDrawHeader drawInRect:renderingRectHeader withFont:fontHeader lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
//Proposed change that resulted in an error
[textToDrawHeader drawInRect:withAttributes:renderingRectHeader withFont:fontHeader lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];发布于 2017-04-03 15:24:07
-boundingRectWithSize:options:attributes:context:的返回类型为CGRect。
例如:
CGRect textRect = [_lbl_title.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];示例:
- (float)expectedLabelHeight
{
CGSize boundingSize = CGSizeMake(CGRectGetWidth(_lbl_title.bounds), CGFLOAT_MAX);
UIFont *font = [UIFont fontWithName:@"Roboto-Light" size:14];
CGRect textRect = [_lbl_title.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];
return (textRect.size.height+20.0);
}根据上面的示例更改代码。
https://stackoverflow.com/questions/43161791
复制相似问题