Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

In iOS 6 i was using this method:

[self.handText sizeWithFont:font 
 minFontSize:10.0f 
 actualFontSize:&maxFontSize 
 forWidth:handWidth/2 
 lineBreakMode:UILineBreakModeClip];

xcode 5 says that 'sizeWithFont:minFontSIze:actualFontSize:forWidth:lineBreakMode:' is deprecated:first deprecated in iOS 7

Now i implemented like this:

[self.handText sizeWithAttributes:@{NSFontAttributeName:font} 
 minFontSize:10.0f 
 actualFontSize:&maxFontSize 
 forWidth:handWidth/2 
 lineBreakMode:NSLineBreakByClipping];

here xcode throws another warning saying: 'Instance method -sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'not found(return type defaults to 'id')

Can anyone please help me to fix this warning.

like image 284
suji Avatar asked Dec 26 '13 13:12

suji


2 Answers

Use this helper method instead:

-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode  {

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = lineBreakMode;

    NSDictionary * attributes = @{NSFontAttributeName:font,
                                  NSParagraphStyleAttributeName:paragraphStyle
                                  };


    CGRect textRect = [text boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attributes
                                         context:nil];

    //Contains both width & height ... Needed: The height
    return textRect.size;
}

Use like so, if you need to support both iOS 6 and iOS 7:

#ifdef __IPHONE_7_0

     titleSize = [self frameForText:self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight) lineBreakMode:self.titleLabel.lineBreakMode ];

     subtitleSize = [self frameForText:self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font  constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight) lineBreakMode:self.subtitleLabel.lineBreakMode];

#else


     titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
                                        constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight)
                                            lineBreakMode:self.titleLabel.lineBreakMode];

     subtitleSize =   [self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font
                                              constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight)
                                                  lineBreakMode:self.subtitleLabel.lineBreakMode];
#endif
like image 99
7c9d6b001a87e497d6b96fbd4c6fdf Avatar answered Oct 23 '22 08:10

7c9d6b001a87e497d6b96fbd4c6fdf


The method signature is:

- (CGSize)sizeWithAttributes:(NSDictionary *)attrs

which means you cannot specify any more argument than the first one (array of attributes). So, you are basically using a method (sizeWithAttributed:minFontSize:forWidth:lineBreakMode:) which is not existing in the iOS SDK.

For a workaround, please have a look at this question.

like image 2
sergio Avatar answered Oct 23 '22 09:10

sergio