iosobjective-cnsvalue

Getting values from NSValue that was created using sizeWithAttributes


I have an NSValue that I am returning from an NSDictionary. The NSValue was created using sizeWithAttributes. I would like to know how to get the NSValue back into a value where I can use it to create the label size.

I get the size of the label calculated from this

betweensLabelSize = [betweensString sizeWithAttributes: @{
                      NSFontAttributeName: [UIFont systemFontOfSize:14.5f]
                }];

I put this into the NSDictionary as a NSValue and retrieve like so

NSValue *tempSize = [currAxisDictionary objectForKey:@"betweensLabelSize"];

which returns this value

NSSize: {64.278503, 86.4925}

Solution

  • To be clear, you presumably created the NSValue using +[NSValue valueWithCGSize:]. -sizeWithAttributes: didn't create the NSValue; it returned a CGSize which you wrapped in an NSValue.

    Anyway, you can get the CGSize out of the NSValue object by calling -CGSizeValue on it:

    CGSize size = [tempSize CGSizeValue];