在iOS 7中获取NSString的高度

5

我正在使用以下代码来计算标签从字符串长度中的高度。我使用的是xcode 5.0,在iOS 6模拟器中运行良好,但在iOS 7中效果不佳。

NSString* str = [[array objectAtIndex:i]valueForKey:@"comment"];

CGSize size = [className sizeWithFont:[UIFont systemFontOfSize:15]   
 constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

Height_1 = size.height;

如果有iOS 7的解决方案,请帮忙一下。 提前感谢。


3
我已经用完了投票次数,但这里有重复的内容:https://dev59.com/X2Mk5IYBdhLWcg3w3xlY 这个也可能有用:https://dev59.com/UmMk5IYBdhLWcg3w3xhY - Mick MacCallum
4个回答

21

这里是我用于计算iOS 6和iOS 7高度的解决方案,并且我传递了一些参数以使其可重复使用。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

/**
*  This method is used to calculate height of text given which fits in specific width having    font provided
*
*  @param text       Text to calculate height of
*  @param widthValue Width of container
*  @param font       Font size of text
*
*  @return Height required to fit given text in container
*/

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
{
    CGFloat result = font.pointSize + 4;
    if (text)
    {
        CGSize textSize = { widthValue, CGFLOAT_MAX };       //Width and height of text area
        CGSize size;
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            //iOS 7
            CGRect frame = [text boundingRectWithSize:textSize
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{ NSFontAttributeName:font }
                                          context:nil];
            size = CGSizeMake(frame.size.width, frame.size.height+1);
        }
        else
        {
            //iOS 6.0
            size = [text sizeWithFont:font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
        }
        result = MAX(size.height, result); //At least one row
    }
    return result;
}

希望这可以帮到你,如果有任何建议都会受到欢迎。Happy Coding :)

iOS 7及以上请使用以下方法。

+ (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
    CGSize size = CGSizeZero;
    if (text) {
        //iOS 7
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height + 1);
    }
    return size;
}

感谢 @Maverick 的编辑。 - Pratik Mistry
你为什么在这里加上了 +1size = CGSizeMake(frame.size.width, frame.size.height+1); - Awesome-o
我在我的应用程序中使用了自定义字体,这导致了轻微的高度问题,所以我添加了+1来解决这个问题。 - Pratik Mistry

2

试着使用这个

#define FONT_SIZE 15.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f


NSString *text;
CGSize constraint;
CGSize size;
CGFloat height;

text = [[array objectAtIndex:i]valueForKey:@"comment"];
constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGRect textRect = [text boundingRectWithSize:constraint
                                    options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                    context:nil];

size = textRect.size;


height = size.height;

1
+1 对于 boundingRectWithSize:options:attributes:context:,-1 对于使用定义而不是 const 静态变量。 - zaph
请注意,boundingRectWithSize:options:attributes:context:仅适用于iOS7及以上版本。 - zaph
@Zaph 感谢您的反馈。我是iOS编码的新手。我应该使用静态变量来替代定义吗?它们会让我的代码执行得更快吗?或者它们实际上是做什么的。请指导一下。 - Maverick
https://dev59.com/onI-5IYBdhLWcg3wxruQ - Mick MacCallum
@0x7fffffff 感谢您的解释。 - Maverick

1
sizeWithFont

已被弃用。请使用

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];

代替


-1

使用以下代码获取标签的高度

CGSize szMaxCell = CGSizeMake(220, 2999);
UIFont *font = [UIFont systemFontOfSize:14.0f];    // whatever font you're using to display
CGSize szCell = [yourString sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByWordWrapping];

1
你建议OP做他们已经尝试过的事情。 - Mick MacCallum
1
sizeWithFont:constrainedToSize:lineBreakMode: 在iOS7中已被弃用,请使用:boundingRectWithSize:options:attributes:context: - zaph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接