Replacement for deprecated sizeWithFont: in iOS 7?

后端 未结 20 989
难免孤独
难免孤独 2020-11-22 08:49

In iOS 7, sizeWithFont: is now deprecated. How do I now pass in the UIFont object into the replacement method sizeWithAttributes:?

相关标签:
20条回答
  • 2020-11-22 08:51

    In iOS7 I needed the logic to return the correct height for the tableview:heightForRowAtIndexPath method, but the sizeWithAttributes always returns the same height regardless of the string length because it doesn't know that it is going to be put in a fixed width table cell. I found this works great for me and calculates the correct height taking in consideration the width for the table cell! This is based on Mr. T's answer above.

    NSString *text = @"The text that I want to wrap in a table cell."
    
    CGFloat width = tableView.frame.size.width - 15 - 30 - 15;  //tableView width - left border width - accessory indicator - right border width
    UIFont *font = [UIFont systemFontOfSize:17];
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    size.height = ceilf(size.height);
    size.width  = ceilf(size.width);
    return size.height + 15;  //Add a little more padding for big thumbs and the detailText label
    
    0 讨论(0)
  • 2020-11-22 08:54

    You can still use sizeWithFont. but, in iOS >= 7.0 method cause crashing if the string contains leading and trailing spaces or end lines \n.

    Trimming text before using it

    label.text = [label.text stringByTrimmingCharactersInSet:
                 [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    

    That's also may apply to sizeWithAttributes and [label sizeToFit].

    also, whenever you have nsstringdrawingtextstorage message sent to deallocated instance in iOS 7.0 device it deals with this.

    0 讨论(0)
  • 2020-11-22 08:57

    Use sizeWithAttributes: instead, which now takes an NSDictionary. Pass in the pair with key UITextAttributeFont and your font object like this:

    CGRect rawRect = {};
    rawRect.size = [string sizeWithAttributes: @{
        NSFontAttributeName: [UIFont systemFontOfSize:17.0f],
    }];
    
    // Values are fractional -- you should take the ceil to get equivalent values
    CGSize adjustedSize = CGRectIntegral(rawRect).size;
    
    0 讨论(0)
  • 2020-11-22 08:57

    I created a category to handle this problem, here it is :

    #import "NSString+StringSizeWithFont.h"
    
    @implementation NSString (StringSizeWithFont)
    
    - (CGSize) sizeWithMyFont:(UIFont *)fontToUse
    {
        if ([self respondsToSelector:@selector(sizeWithAttributes:)])
        {
            NSDictionary* attribs = @{NSFontAttributeName:fontToUse};
            return ([self sizeWithAttributes:attribs]);
        }
        return ([self sizeWithFont:fontToUse]);
    }
    

    This way you only have to find/replace sizeWithFont: with sizeWithMyFont: and you're good to go.

    0 讨论(0)
  • 2020-11-22 08:57
    CGSize maximumLabelSize = CGSizeMake(label.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    float heightUse = expectedLabelSize.height;
    
    0 讨论(0)
  • 2020-11-22 08:57

    Try this syntax:

    NSAttributedString *attributedText =
        [[NSAttributedString alloc] initWithString:text 
                                        attributes:@{NSFontAttributeName: font}];
    
    0 讨论(0)
提交回复
热议问题