Replacement for deprecated sizeWithFont: in iOS 7?

后端 未结 20 1358
难免孤独
难免孤独 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 09:01

    As you can see sizeWithFont at Apple Developer site it is deprecated so we need to use sizeWithAttributes.

    #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    
    NSString *text = @"Hello iOS 7.0";
    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        // code here for iOS 5.0,6.0 and so on
        CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" 
                                                             size:12]];
    } else {
        // code here for iOS 7.0
       CGSize fontSize = [text sizeWithAttributes: 
                                @{NSFontAttributeName: 
                                  [UIFont fontWithName:@"Helvetica" size:12]}];
    }
    

提交回复
热议问题