从sizewithFont转换:constrainedToSize:lineBreakMode:对于iOS 7不按预期运行

时间:2013-12-28 02:17:42

标签: ios objective-c ios7

我正在尝试摆脱iOS 7中已弃用的功能,而我正在运行一些我不理解的功能。这是我现在的代码:

CGFloat titleHeight = [[object name] sizeWithFont:[UIFont fontWithName:@"Abel" size:24.0f] constrainedToSize:CGSizeMake(170.0f, 200.0f) lineBreakMode:NSLineBreakByWordWrapping].height;
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
CGFloat titleHeight2 = [[object name] boundingRectWithSize:CGSizeMake(170.0f, 200.0f) options:0 attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Abel" size:24.0f], NSParagraphStyleAttributeName: paragraphStyle}  context:nil].size.height;

出于某种原因,titleHeighttitleHeight2最终会成为不同的值,我无法弄清楚原因。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

文档says

  

要正确绘制和调整多行文字,请传递   选项参数中的NSStringDrawingUsesLineFragmentOrigin

你试过吗?