Question

I have code which has a ton of sizeWithFont calls that I need to desperately get rid off since sizeWithFont has been deprecated. Now, I have sort of figured out how to use boundingRectWithSize in its stead, however, instead of re-doing a million calls, I thought of making a wrapper function to do the sizeWithFonts.

So, here's the method I came up with :

- (CGSize) getSizeForCurrentFont:(UIFont*)font
                    forWidth:(CGFloat)width
           constrainedToSize:(CGSize)size
               lineBreakMode:(NSLineBreakMode)lineBreakMode
                 minFontSize:(CGFloat)minFontSize
              actualFontSize:(CGFloat *)actualFontSize
{
    // functionality
}

What I am going for is that only the values that are set will be called while the rest will be whatever the default value for them should be. So, if a call only has font and width, only that will be set. I do understand that fontSize is no longer supported, but I'd appreciate suggestions on that.

My question is : what are the default values that I should set font,width,size,lineBreakMode,minFontSize and actualFontSize to so that I don't get weird results. Essentially, I want the same result for :

[s sizeWithFont:font
constrainedToSize:size];

as with :

[s getSizeForCurrentFont:font 
forWidth:width #This is some Default Value# 
constrainedToSize:size 
lineBreakMode:lineBreakMode #This is some Default Value# 
minFontSize:minFontSize #This is some Default Value# 
actualFontSize: actualFontSize #This is some Default Value#]

Do tell me if any further clarification is required. If you can suggest a nice way to do boundingRectWithSize, then that would be cool as well.

Was it helpful?

Solution

You could wrap primitives into objects? So you'd have:

- (CGSize) getSizeForCurrentFont:(UIFont*)font
                    forWidth:(NSNumber*)width
           constrainedToSize:(NSValue*)size
               lineBreakMode:(NSNumber*)lineBreakMode
                 minFontSize:(NSNumber*)minFontSize
              actualFontSize:(NSNumber*)actualFontSize
{
    // functionality
}

Any 'default' values would be set to nil. For CGFloat, you'd create them using:

CGFloat value = 10.0f;
NSNumber* valueObj = @(value);

For NSValue, you can use:

CGSize size = CGSizeMake(10.0f, 10.0f);
NSValue* sizeObj = [NSValue valueWithCGSize:size];

The key issue here, is how to differentiate between a valid value and an absence identifier. Typically you do this using either a defined constant (such as NSNotFound), but for the types you've listed such a thing doesn't exist. This is where objects can help, as the lack of an object inherently indicates the absence of a value.

OTHER TIPS

you can create a method:

- (CGSize) sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size {
    [self getSizeForCurrentFont:font 
        forWidth:#some Default Value# 
        constrainedToSize:size 
        lineBreakMode:#some Default Value# 
        minFontSize:#some Default Value# 
        actualFontSize:#some Default Value#]
}

This is the simpliest way of creating "default values".

Update Swift 4.2

 let size = CGSize(width: 0, height: 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top