【问题标题】:sizewithFont and drawinRect FormattingsizewithFont 和 drawinRect 格式化
【发布时间】:2017-04-03 15:24:07
【问题描述】:

我有一个 iPhone 应用程序,并且每年都会有一些代码元素产生已弃用的问题。除了一些轻微的格式问题外,我的应用程序似乎运行良好。我尝试使用建议的代码,但它只会导致错误。我真的很想解决这些问题,看看是否能解决格式问题。有人可以帮我解决这些问题。

第一个问题:'sizeWithFont:constrainedToSize:lineBreakMode:' 已弃用:在 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];

【问题讨论】:

  • 您是否阅读了警告信息?他们告诉你改用什么。并请搜索消息。这些已经在这里介绍过很多次了。
  • 我确实搜索了如何使用替换,但鉴于给出的示例,不清楚如何做到这一点。
  • Edit 您的问题带有实际代码(不是图片),显示您尝试执行的操作。清楚地解释您尝试遇到的问题。
  • 分享您的代码,以帮助您,但正如警告中所说,您需要用新方法替换不推荐使用的方法
  • 下面是我的代码。

标签: ios iphone


【解决方案1】:

-boundingRectWithSize:options:attributes:context: 的返回类型为CGRect。但是在您的代码中,您将值分配给CGSize

例如:

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

按照上面的例子修改你的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多