sizeWithFont: in iOS 7过期后如何替换

以前的写法

1 NSString *text = ...;
2 CGFloat width = ...;
3 UIFont *font = ...;
4 CGSize size = [text sizeWithFont:font 
5                constrainedToSize:(CGSize){width, CGFLOAT_MAX}];

替换成新的写法

 1 NSString *text = ...;
 2 CGFloat width = ...;
 3 UIFont *font = ...;
 4 NSAttributedString *attributedText =
 5     [[NSAttributedString alloc]
 6         initWithString:text
 7         attributes:@
 8         {
 9             NSFontAttributeName: font
10         }];
11 CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
12                                            options:NSStringDrawingUsesLineFragmentOrigin
13                                            context:nil];
14 CGSize size = rect.size;

 

posted @ 2015-04-17 10:05  流觞若火  阅读(2711)  评论(0编辑  收藏  举报