Error: bad receiver type 'CGSize' (aka 'struct CGSize') | Xcode 6.4 | iOS 8.4

Error: bad receiver type 'CGSize' (aka 'struct CGSize') | Xcode 6.4 | iOS 8.4

我遇到了一个已弃用方法的问题,我正在尝试将其更改为现在可用的方法,但没有成功,有人知道如何解决这个问题吗?

问题是我正在处理一个不属于我的代码,所以我不太理解它。

这是原始代码(已弃用的代码):

CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin; 
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
                            constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];

这是我的解决方案(抛出错误):

CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin; 
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [[detailsLabel.text sizeWithAttributes:@{NSFontAttributeName:detailsLabel.font}]constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];

错误在标题中(接收器类型错误 'CGSize'(又名 'struct CGSize')

非常感谢!

您的问题出在这一行:

CGSize detailsLabelSize = [[detailsLabel.text sizeWithAttributes:@{NSFontAttributeName:detailsLabel.font}]constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];

[detailsLabel.text sizeWithAttributes:@{NSFontAttributeName:detailsLabel.font}] returns a CGSize 这不是一个对象。然后你试图在这个 CGSize 上调用 constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode,这是不可能的,因为它不是一个对象。

在您的实现文件中添加此方法:

-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode  {

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = lineBreakMode;

    NSDictionary * attributes = @{NSFontAttributeName:font,
                                  NSParagraphStyleAttributeName:paragraphStyle
                                  };


    CGRect textRect = [text boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attributes
                                         context:nil];
    return textRect.size;
}

然后将您的代码更改为:

CGSize detailsLabelSize = [self frameForText:detailsLabel.text sizeWithFont:detailsLabel.font constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];