Getting values from NSValue that was created using sizeWithAttributes

1.6k views Asked by At

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}
2

There are 2 answers

3
Ken Thomases On BEST ANSWER

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];
4
Hussain Shabbir On

Try below code for accessing the size value:-

NSValue *val=[mut objectForKey:@"betweensLabelSize"];
CGSize sz=val.CGSizeValue;