本文介绍了NSString sizeWithFont:iOS7中的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法(sizeWithFont:forWidth:lineBreakMode:)在iOS 7.0中已被弃用。

The method (sizeWithFont: forWidth: lineBreakMode:) was deprecated in iOS 7.0.

但怎么能我跟iOS 7.0中的代码一样吗?

But how can I do a same thing like following code in iOS 7.0?

CGSize fontSize =[self.text sizeWithFont:self.font forWidth:self.frame.size.width lineBreakMode:NSLineBreakByTruncatingTail];

可以(boundingRectWithSize:options:attributes:context :) 这样做?我不确定,但这是我在Apple文档上搜索的方法。

Can (boundingRectWithSize:options:attributes:context:) do this? I am not sure but this is the method I searched on apple document.

感谢您的帮助。

推荐答案

我有一个NSString类别来获取字符串的宽度或高度:

I have got a category for NSString to get the width or heigth of a string:

- (CGFloat)widthWithFont:(UIFont *)font
{
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    return [[[NSAttributedString alloc] initWithString:self attributes:attributes] size].width;
}

- (CGFloat)heigthWithWidth:(CGFloat)width andFont:(UIFont *)font
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self];
    [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [self length])];
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    return rect.size.height;
}

这篇关于NSString sizeWithFont:iOS7中的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 01:55