2014-02-27 62 views
0

有人知道如何在這種情況下使用-boundingRectWithSize:options:attributes:context:替代棄用的「sizeWithFont:constrainedToSize:」方法。棄用'sizeWithFont:constrainedToSize:'替換

CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))]; 

獲取警告: 'sizeWithFont:constrainedToSize:' 已過時:在IOS 7.0第一棄用 - 使用-boundingRectWithSize:選項:屬性:背景:

這是孔這段代碼:

// calculate the label size 

CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))]; 

each_object(self.labels, ^(UILabel *label) { 
    CGRect frame = label.frame; 
    frame.origin.x = offset; 
    frame.size.height = CGRectGetHeight(self.bounds); 
    frame.size.width = labelSize.width + 2.f /*Magic number*/; 
    label.frame = frame; 


    // Recenter label vertically within the scroll view 
    label.center = CGPointMake(label.center.x, roundf(self.center.y - CGRectGetMinY(self.frame))); 

    offset += CGRectGetWidth(label.bounds) + self.labelSpacing; 
}); 

回答

2

在你的那一刻...

CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))]; 

因此,使用...

CGRect boundingRect = [self.mainLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds)) 
                 options:NSStringDrawingUsesLineFragmentOrigin 
                 context:nil]; 

CGSize labelSize = boundingRect.size; 

這應該有效。

或者...帶屬性...

CGRect boundingRect = [self.mainLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds)) 
                 options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:@{NSFontAttributeName:self.mainLabel.font} 
                 context:nil]; 
+0

謝謝!但我得到這個「沒有可見的@interface爲'NSString'聲明選擇器'boundingRectWithSize:options:context:'」 – user2023660

+0

你可以在這裏看到它的文檔... https://developer.apple.com/library/ios /documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html – Fogmeister

+0

我應該替換還是應該放在某處?在參考文獻中不要更清楚。 – user2023660

0

例如這樣

-(CGFloat)getLabelSize:(UILabel *)label fontSize:(NSInteger)fontSize 
{ 


    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
             [UIFont systemFontOfSize:fontSize], NSFontAttributeName, 
             nil]; 

    CGRect frame = [label.text boundingRectWithSize:CGSizeMake(270, 2000.0) 
             options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:attributesDictionary 
             context:nil]; 

    CGSize size = frame.size; 

    return size.height; 
} 
+0

謝謝! 'getLabelSize'上有一些未聲明的標識符。對不起,我有一個新手,我該如何解決這個問題? – user2023660

+0

你是怎麼稱呼這種方法的? – nerowolfe

+0

來自InfoViewController.m – user2023660

相關問題