Replacement for deprecated sizeWithFont: in iOS 7?

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

    // max size constraint
    CGSize maximumLabelSize = CGSizeMake(184, FLT_MAX)
    
    // font
    UIFont *font = [UIFont fontWithName:TRADE_GOTHIC_REGULAR size:20.0f];
    
    // set paragraph style
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    // dictionary of attributes
    NSDictionary *attributes = @{NSFontAttributeName:font,
                                 NSParagraphStyleAttributeName: paragraphStyle.copy};
    
    CGRect textRect = [string boundingRectWithSize: maximumLabelSize
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attributes
                                         context:nil];
    
    CGSize expectedLabelSize = CGSizeMake(ceil(textRect.size.width), ceil(textRect.size.height));
    

提交回复
热议问题