sizewithFont和drawinRect格式

时间:2017-04-01 20:40:59

标签: ios iphone

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

第一个问题:'sizeWithFont:constrainedToSize:lineBreakMode:'不推荐使用:首先在iOS 7.0中弃用 - 使用-boundingRectWithSize:options:attributes:context:尝试使用建议的替换,但它只是导致错误(请参阅代码下面)。不确定将当前代码放在选项,属性和上下文中的哪个位置。

第二期:'drawInRect:withFont:lineBreakMode:alignment:'不推荐使用:首先在iOS 7.0中弃用 - 使用-drawInRect:withAttributes:尝试使用建议的替换,但它只是导致错误(请参阅下面的代码) 。不确定当前代码与atAttributes的匹配位置。

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

1 个答案:

答案 0 :(得分:0)

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

按照上面的例子更改您的代码。