Question

Previously in my application i was using sizeWithFont like this.

Im trying to adjust the size and position of an image according to the text label width and position. so i try i like this,

CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithFont:self.categoryLabel.font].width + 10, y, _imageSize.width, _imageSize.height);

But in iOS 7 it is deprecated and asked to sizeWithAttributes.

When i tried like this i was getting the "x" cordinate like this. Before deprecating

But when i use the code like this CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.frame.size.width+10]}],y,_imageSize.width,_imageSize.height);

im getting the "x" cordinate like this.

After deprecation

Can anyone please help me on this.

Was it helpful?

Solution

Replace this code:

self.categoryLabel.font.width+10

with

self.categoryLabel.frame.size.width+10

There is no property width on font. I assume you want to get width from frame of your label.

// EXTENDED

This is an example of whole rect creation line, CGRect expect 4 parameters, I listed it line by line separated by coma, I'm not sure is it what you need but it will give you some result:

CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.font.pointSize]}].width, //<- this .width takes width ot the size with font but maybe you required height
y,
_imageSize.width,
_imageSize.height);

If you have a look on the line below (your old one) there is not 4 parameters:

CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[self.categoryLabel.frame.size.width]+10,y,_imageSize.width,_im‌​ageSize.height]}];

it looks like it's just one so it won't work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top