微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可可 – NSString sizeWithAttributes:content rect

如何获得Nsstring的大小,就像它在一个NSRect中一样.问题是当我尝试 – [Nsstring sizeWithAttributes:],它返回一个NSSize,就好像它有无限宽度.我想给方法最大的宽度.有什么办法吗(BTW:Mac OS,而不是iPhone操作系统)

谢谢,
亚历克斯

解决方法

float heightForStringDrawing(Nsstring *myString,NSFont *myFont,float myWidth)
{
 NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease];
 NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth,FLT_MAX)] autorelease];
 ;
        NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease];
 [layoutManager addTextContainer:textContainer];
 [textStorage addLayoutManager:layoutManager];
 [textStorage addAttribute:NSFontAttributeName value:myFont
      range:NSMakeRange(0,[textStorage length])];
 [textContainer setLineFragmentPadding:0.0];

 (void) [layoutManager glyphRangeForTextContainer:textContainer];
 return [layoutManager
   usedRectForTextContainer:textContainer].size.height;
}

毕竟这是文件.谢谢约书亚!

原文地址:https://www.jb51.cc/iOS/335052.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐