sizeWithFont:替换

时间:2013-09-30 10:09:57

标签: ios nsstring ios7 nsattributedstring

iOS 7中不推荐使用以下NSString方法:

    - (CGSize)sizeWithFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -sizeWithAttributes:");

    - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:
(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");

    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");

    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); 

虽然替换第一个是直截了当的,但其余的并不是那么多。对boundingRectWithSize:options:attributes:context:获得完全相同结果的等效调用是什么?

特别是:

  • options的价值应该是什么?
  • 如何指定lineBreakMode

2 个答案:

答案 0 :(得分:1)

。如何指定lineBreakMode?

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr addAttribute:NSParagraphStyleAttributeName
                 value:paragraphStyle
                 range:NSMakeRange(0,[attributedStr length])];

答案 1 :(得分:1)

添加此

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;