简体   繁体   English

sizeWithFont:在iOS 7中

[英]sizeWithFont: in iOS 7

I am getting a warning: "'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0" Can anyone please suggest me an alternative for this method? 我收到警告:“不推荐使用'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;
}  

The alternative is: 替代方法是:

- (NSSize)sizeWithAttributes:(NSDictionary *)attributes

In your case: 在您的情况下:

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont fontwithName:@"SeroCompPro-Light" size:14]}];

This function is deprecated in ios 7. Instead of this function ios 7中不推荐使用此功能。

sizeWithFont:constrainedToSize:lineBreakMode sizeWithFont:constrainedToSize:lineBreakMode

use this function, Use 使用此功能,使用

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

 boundingRectWithSize:options:attributes:context:

Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context. 在当前图形上下文的指定矩形内,计算并返回使用给定选项和显示特性绘制的接收器的边界矩形。

Parameters size The size of the rectangle to draw in. 参数size要绘制的矩形的大小。

options String drawing options. options字符串绘制选项。

attributes A dictionary of text attributes to be applied to the string. 属性应用于字符串的文本属性字典。 These are the same attributes that can be applied to an NSAttributedString object, but in the case of NSString objects, the attributes apply to the entire string, rather than ranges within the string. 这些是可以应用于NSAttributedString对象的相同属性,但是对于NSString对象,这些属性适用于整个字符串,而不是字符串中的范围。 context 上下文

The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments. 用于接收器的字符串绘制上下文,指定最小比例因子和跟踪调整。

I used bellow code and it works: 我用下面的代码,它的工作原理:

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));

Problem at creating a NSAttributedString and get properly width height with ceilf method 创建NSAttributedString并使用ceilf方法获得正确的宽度高度时出现问题

Alternate solution for supporting IOS7 and lower version- 支持IOS7和更低版本的备用解决方案-

CGSize expectedLabelSize;
if ([self respondsToSelector:@selector(sizeWithAttributes:)])
{
    expectedLabelSize = [subTitle sizeWithAttributes:@{NSFontAttributeName:subTitleLabel.font}];
}else{
    expectedLabelSize = [subTitle sizeWithFont:subTitleLabel.font constrainedToSize:subTitleLabel.frame.size lineBreakMode:NSLineBreakByWordWrapping];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM