Replacement for deprecated sizeWithFont: in iOS 7?

后端 未结 20 1053
难免孤独
难免孤独 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: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.

提交回复
热议问题