Joelistic
Joelistic

Reputation: 1

Problems through the sizeWithFont depreciated in iOS7.0 in my code. Don't understand how to replace it with boundingRectWithSize

So as I need to upgrade my app to newer iOS i came across the issue with sizeWithFont command being depreciated. Can someone please help me out on how to replace it with the new Function.

Here is my code:

  // Add label if label text was set
if (nil != self.labelText) {
    // Get size of label text
    CGSize dims = [self.labelText sizeWithFont:self.labelFont];

    // Compute label dimensions based on font metrics if size is larger than max then clip the label width
    float lHeight = dims.height;
    float lWidth;
    if (dims.width <= (frame.size.width - 2 * margin)) {
        lWidth = dims.width;
    }
    else {
        lWidth = frame.size.width - 4 * margin;
    }

    // Set label properties
    label.font = self.labelFont;
    label.adjustsFontSizeToFitWidth = NO;
    label.textAlignment = NSTextAlignmentCenter;
    label.opaque = NO;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = self.labelText;

How do I need to use the new Function to get the size of the label text?

Upvotes: -1

Views: 67

Answers (2)

poojathorat
poojathorat

Reputation: 1220

Use sizeWithAttributes method

CGSize size = [string sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];

// Values are fractional -- you should take the ceilf to get equivalent values
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));

Upvotes: 0

Ashish Kakkad
Ashish Kakkad

Reputation: 23892

Use boundingRectWithSize as follows :

CGSize sizeT = CGRectIntegral([yourstring boundingRectWithSize:CGSizeMake(yourpreferedwidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;

or you can use sizeThatFits

CGSize maximumLabelSize = CGSizeMake(310, 9999);
CGSize expectSize = [yourlabel sizeThatFits:maximumLabelSize];

For more see this answer : https://stackoverflow.com/a/19518480/3202193

Upvotes: 0

Related Questions