我收到了一个警告:"'sizeWithFont:constrainedToSize:lineBreakMode:‘是不推荐的:首先,在iOS 7.0中不推荐:“有人能给我推荐一种替代方法吗?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Calculate height based on cell content — cell content will stretch appropriately when the height is set
Post *post = self.articleComments[indexPath.row];
CGFloat width = tableView.frame.size.width - 71 - 15; // Width of comment text area
UIFont *commentFont = [UIFont fontWithName:@"SeroCompPro-Light" size:14];
CGFloat commentTextHeight = [post.text sizeWithFont:commentFont constrainedToSize:CGSizeMake(width, 10000) lineBreakMode:NSLineBreakByWordWrapping].height;
return commentTextHeight + 31 + 37;
} 发布于 2013-09-27 08:11:16
此函数在ios 7中不推荐使用。
sizeWithFont:constrainedToSize:line根模式
使用这个函数,使用
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
boundingRectWithSize:options:attributes:context:在当前图形上下文中指定的矩形内,计算并返回使用给定选项和显示特性绘制的接收器的边框。
参数大小为要绘制的矩形的大小。
选项字符串绘制选项。
要应用于字符串的文本属性字典。这些属性可以应用于NSAttributedString对象,但对于NSString对象,属性适用于整个字符串,而不是字符串中的范围。上下文
用于接收方的字符串绘图上下文,指定最小比例尺因子和跟踪调整。
发布于 2013-09-27 07:48:47
另一种选择是:
- (NSSize)sizeWithAttributes:(NSDictionary *)attributes就你而言:
[string sizeWithAttributes:@{NSFontAttributeName:[UIFont fontwithName:@"SeroCompPro-Light" size:14]}];发布于 2013-10-09 23:31:22
我使用了下面的代码,它起作用了:
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){size.width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
result = CGSizeMake(ceilf(rect.size.width), ceilf(rect.size.height));创建NSAttributedString并使用ceilf方法获得适当宽度高度的问题
https://stackoverflow.com/questions/19045138
复制相似问题