【问题标题】:Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7替换 ios 7 中弃用的 sizeWithFont:minFontSIze:actualFontSize
【发布时间】:2014-01-14 04:22:30
【问题描述】:

在 iOS 6 中我使用了这种方法:

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

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

现在我是这样实现的:

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

这里 xcode 抛出另一个警告说: 'Instance method -sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'not found(return type defaults to 'id')

谁能帮我解决这个警告。

【问题讨论】:

    标签: iphone ios7 uifont


    【解决方案1】:

    你可以用这个方法

    - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
    

    【讨论】:

      【解决方案2】:

      方法签名是:

      - (CGSize)sizeWithAttributes:(NSDictionary *)attrs
      

      这意味着您不能指定比第一个参数(属性数组)更多的参数。因此,您基本上使用的是 iOS SDK 中不存在的方法 (sizeWithAttributed:minFontSize:forWidth:lineBreakMode:)。

      如需解决方法,请查看this question

      【讨论】:

        【解决方案3】:

        改用这个辅助方法:

        -(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;
        }
        

        这样使用,如果你需要同时支持 iOS 6 和 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
        

        【讨论】:

        • 这不是等效的解决方案,是吗?如果actualFontSize 不符合约束条件,提问者最初使用的方法将测试各种大小。这个答案中的解决方案似乎没有这样做......?
        猜你喜欢
        • 2013-09-25
        • 2013-09-24
        • 2015-03-06
        • 2015-06-18
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多