如何在iOS 6中计算具有给定宽度的NSAttribute字符串的高度

如何在iOS 6中计算具有给定宽度的NSAttribute字符串的高度,ios,nsattributedstring,bounding-box,Ios,Nsattributedstring,Bounding Box,可能重复: 现在,NSAttributedString在iOS 6中可用。出于布局目的,我想知道如何在固定宽度下计算NSAttributedString所需的高度。我正在寻找与NSString的-(CGSize)sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size等效的东西,但用于NSAttributedString 要计算NSAttribute字符串的图形大小,有两种方法可用: -(CGSize)size无法使用,因为它没有考虑任

可能重复:

现在,NSAttributedString在iOS 6中可用。出于布局目的,我想知道如何在固定宽度下计算NSAttributedString所需的高度。我正在寻找与NSString的
-(CGSize)sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size
等效的东西,但用于NSAttributedString

要计算NSAttribute字符串的图形大小,有两种方法可用:

  • -(CGSize)size
    无法使用,因为它没有考虑任何宽度
  • 我尝试了
    -(CGRect)boundingrectiwithsize:(CGSize)size选项:(NSStringDrawingOptions)选项上下文:(NSStringDrawingContext*)上下文
    ,但不知怎的,它没有给我正确的高度。我认为这个方法有缺陷。如果我运行以下代码,它会给出
    边界大小:572.324951,19.000000
    忽略给定的宽度200。它应该给我大约100的高度
  • NSMutableAttributedString*attributedString=[[NSMutableAttributedString alloc]init]; NSDictionary*attributes=@{NSFontAttributeName:[UIFontFontWithName:@“HelveticaNeue”大小:15],NSForegroundColorAttributeName:[UIColor blueColor]}; [attributedString AppendedAttributedString:[[NSAttributedString alloc]initWithString:@“属性字符串\n”属性:属性]]; [attributedString AppendedAttributedString:[[NSAttributedString alloc]initWithString:@“属性字符串\n”属性:属性]]; [attributedString AppendedAttributedString:[[NSAttributedString alloc]initWithString:@“属性字符串\n”属性:属性]]; [attributedString AppendedAttributedString:[[NSAttributedString alloc]initWithString:@“属性字符串\n”属性:属性]]; [attributedString AppendedAttributedString:[[NSAttributedString alloc]initWithString:@“属性字符串\n”属性:属性]]; CGRect frame=[attributedString BoundingDirectWithSize:CGSizeMake(2001000)选项:0上下文:nil]; NSLog(@“边界大小:%f,%f”,frame.size.width,frame.size.height);
    Mac OS X还有其他可用的方法,但不适用于iOS。

    选项2在具有适当参数的iOS中确实有效

    NSAttributedString *attrStr = ... // your attributed string
    CGFloat width = 300; // whatever your desired width is
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    
    如果
    选项
    参数没有正确的值,您将获得错误的高度


    还要求
    attrStr
    包含字体属性。没有字体,就无法正确计算大小。

    @SimonGoldeen这不是一个好的副本。接受的答案实际上没有给出高度,其他答案也没有显示使用
    boundingRectWithSize:options:context:
    方法的正确方法。在Swift中,这可以通过
    let desiredWidth:CGFloat=300;让rect=attrStr.boundingRect(带:CGSize(宽度:desiredWidth,高度:CGFloat.greatestFiniteMagnitude),选项:[.usesLineFragmentOrigin.usesFuntleding],上下文:nil)
    Hmm。。。不为我工作。我似乎只有一半的时间能得到很好的计算。最后,一种简单而实用的方法可以计算
    nsattributed字符串的大小,即使格式非常复杂也能工作。类似问题的其他答案甚至可以在NSAttributedString上创建一个类别——谢天谢地,根据我的经验,它不必那么难,NSAttributeString必须有一个NSFontAttributeName键才能正确调整大小。@bentford感谢您提供的提示-我使用-initWithString:方法创建了一个属性字符串,这导致了完全任意的计算大小。使用-initWithString:attributes:works!我们可能希望对size参数使用
    CGFLOAT\u MAX
    ,而不是
    10000
    ,在某个时间点,它可能实际适合我们的屏幕。。。
    NSAttributedString *attrStr = ... // your attributed string
    CGFloat width = 300; // whatever your desired width is
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];