Replacement for deprecated sizeWithFont: in iOS 7?

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

    Alternate solution-

    CGSize expectedLabelSize;
    if ([subTitle respondsToSelector:@selector(sizeWithAttributes:)])
    {
        expectedLabelSize = [subTitle sizeWithAttributes:@{NSFontAttributeName:subTitleLabel.font}];
    }else{
        expectedLabelSize = [subTitle sizeWithFont:subTitleLabel.font constrainedToSize:subTitleLabel.frame.size lineBreakMode:NSLineBreakByWordWrapping];
    }
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
提交回复
热议问题