Replacement for deprecated sizeWithFont: in iOS 7?

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

    Create a function that takes a UILabel instance. and returns CGSize

    CGSize constraint = CGSizeMake(label.frame.size.width , 2000.0);
    // Adjust according to requirement
    
    CGSize size;
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
    
        NSRange range = NSMakeRange(0, [label.attributedText length]);
    
        NSDictionary *attributes = [label.attributedText attributesAtIndex:0 effectiveRange:&range];
        CGSize boundingBox = [label.text boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    
        size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
    }
    else{
        size = [label.text sizeWithFont:label.font constrainedToSize:constraint lineBreakMode:label.lineBreakMode];
    }
    
    return size;
    

提交回复
热议问题