sizeWithFont:constrainedToSize:lineBreakMode: deprecated in iOS7

后端 未结 3 1421
鱼传尺愫
鱼传尺愫 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];
    }
    
    0 讨论(0)
  • 2020-12-18 04:37

    sizeWithFont API you are using is deprecated on iOS7.

    // See UIStringDrawing.h
    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); // NSTextAlignment is not needed to determine size
    

    You can use the API suggestion like so:

    NSMutableDictionary *atts = [[NSMutableDictionary alloc] init];
    [atts setObject:myFont forKey:NSFontAttributeName];
    
    CGRect rect = [myText boundingRectWithSize:constraint
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:atts
                                     context:nil];
    
    0 讨论(0)
  • 2020-12-18 04:41

    If you're only supporting ios6 and later, you can convert your NSStrings to NSAttributedStrings and use NSAttributedString's boundingRectWithSize:options:context:.

    Something that looked like this before:

    CGSize size = [text sizeWithFont:font
                   constrainedToSize:(CGSize){maxWidth, CGFLOAT_MAX}];
    

    Could be easily converted to this and work in both ios6 and ios7:

    NSAttributedString *attributedText =
        [[NSAttributedString alloc]
            initWithString:text
            attributes:@
            {
                NSFontAttributeName:    font
            }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){maxWidth, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    

    As a side note, the benefit of doing things this way is so your text sizing in ios6 is now thread-safe. The old methods of sizeWithFont:... belonged to UIStringDrawing, which would crash if you ran sizeWithFont:... on two threads at the same time. In ios6, new NSStringDrawing functions for NSAttributedStrings were exposed and the boundingRectWithSize:... function is thread safe. I'm guessing this is why in ios7, the old sizeWithFont:... functions were deprecated.

    Please note the documentation mentions:

    In iOS 7 and later, this method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.

    So to pull out the calculated height or width to be used for sizing views, I would use:

    CGFloat height = ceilf(size.height);
    CGFloat width  = ceilf(size.width);
    
    0 讨论(0)
提交回复
热议问题