sizeWithFont:constrainedToSize:lineBreakMode: deprecated in iOS7

后端 未结 3 1431
鱼传尺愫
鱼传尺愫 2020-12-18 04:16

I\'m updating my app to iOS 7 and finally got it, but there\'s one thing I can\'t find a solution for.

In Xcode 4 I used the following method:

#defin         


        
3条回答
  •  生来不讨喜
    2020-12-18 04:25

    sizeWithFont methods were deprecated in iOS7. You should use boundingRectWithSize instead. If you also need to support prior iOS versions then you can use following code:

    CGSize size = CGSizeZero;
    
    if ([label.text respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)] == YES) {
        size = [label.text boundingRectWithSize: constrainedSize options: NSStringDrawingUsesLineFragmentOrigin
                                     attributes: @{ NSFontAttributeName: label.font } context: nil].size;
    } else {
        size = [label.text sizeWithFont: label.font constrainedToSize: constrainedSize lineBreakMode: UILineBreakModeWordWrap];
    }
    

提交回复
热议问题