【问题标题】:How to convert sizeWithFont to sizeWithAttributes(iOS 7)如何将 sizeWithFont 转换为 sizeWithAttributes(iOS 7)
【发布时间】:2014-03-26 06:25:24
【问题描述】:
如何在 iOS 7 SDK 中获取 NSString 的 CGSize 值,只需将以下代码行转换为 sizeWithAttributes。
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
【问题讨论】:
标签:
objective-c
ios7
nsstring
cgsize
【解决方案1】:
你可以试试这个...
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};
[text sizeWithAttributes:attributes]
或
CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];