簡體   English   中英

錯誤:錯誤的接收器類型'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'(aka'struct CGSize')

非常感謝!

您的問題在這一行:

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

[detailsLabel.text sizeWithAttributes:@{NSFontAttributeName:detailsLabel.font}]返回一個不是對象的CGSize 然后,您嘗試在此CGSize上調用constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode CGSize ,這是不可能的,因為它不是對象。

在實現文件中添加此方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM