2014-03-26 140 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];