动态行高和sizeWithAttributes警告

时间:2014-10-13 00:28:26

标签: ios objective-c iphone

好的,我使用sizeWithFont:constrainedToSize功能,我收到编译器警告。我已经读过我需要使用sizeWithAttributes(链接到这里:Replacement for deprecated sizeWithFont: in iOS 7?)。

我很难理解如何动态实现sizeWithAttributes,以便我的单元格行的高度更改为内容高度(请参见下文)。

有关如何摆脱此警告的任何想法?谢谢!

int topPadding = cell.menuItemTitle.frame.origin.x;

int bottomPadding = cell.frame.size.height-
(topPadding+cell.menuItemTitle.frame.size.height);

NSString *text = [[menuItems objectAtIndex:indexPath.row] valueForKey:@"title"];

CGSize maximumSize = CGSizeMake(cell.menuItemTitle.frame.size.width, 9999);

CGSize expectedSize = [text sizeWithFont:cell.menuItemTitle.font 
constrainedToSize:maximumSize lineBreakMode:cell.menuItemTitle.lineBreakMode];

return topPadding+expectedSize.height+bottomPadding;

1 个答案:

答案 0 :(得分:1)

你的topPadding应该是origin.y而不是origin.x。您可以使用以下内容来获取文本的大小。

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:label.font,
                          NSFontAttributeName,
                          paragraphStyle,
                          NSParagraphStyleAttributeName,
                          nil];
CGRect expectedRect = [text boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
CGSize expectedSize = expectedRect.size;