2014-03-26 94 views
4

如何在iOS 7 SDK中获得NSStringCGSize值,只需要将sizeWithAttributes转换为下面的代码行即可。如何将sizeWithFont转换为sizeWithAttributes(iOS 7)

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; 
+0

可能重复[iOS的7 sizeWithAttributes:更换sizeWithFont:constrainedToSize(http://stackoverflow.com /问题/ 19145078/IOS -7- sizewithattributes替换换sizewithfontconstrainedtosize) – BB9z

回答

9

你可以试试这个...

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]}; 

[text sizeWithAttributes:attributes] 

CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];