我应该用什么替换不推荐使用的sizeWithFont:方法? [英] With what should I replace the deprecated sizeWithFont: method?

查看:124
本文介绍了我应该用什么替换不推荐使用的sizeWithFont:方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,可以给我一定长度的字符串(具有相应的正确字体大小)的UITextView完美大小:

I have a method that gives me the perfect size for a UITextView given a length of string (with the corresponding correct font size) :

- (NSInteger) heightOfLabel:(NSString*) string {
    CGSize maximumLabelSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width - 40, FLT_MAX);
    CGSize expectedLabelSize = [[NSString stringTrimmedForLeadingAndTrailingWhiteSpacesFromString:string]
             sizeWithFont:[UIFont systemFontOfSize:15]
             constrainedToSize:maximumLabelSize 
             lineBreakMode:NSLineBreakByWordWrapping];

    return expectedLabelSize.height + 5;
}

实际上,即使在iOS7中,它也仍然非常适合我.尽管现在它提出了一个警告方法,说我不应该使用"sizeWithFont:contrainedToSize:lineBreakMode".

In fact, it still gives me a perfect fit, even in iOS7. Although now it comes up with a warning method that says I shouldn't use 'sizeWithFont:contrainedToSize:lineBreakMode'.

现在它说我应该使用-boundingRectWithSize:options:attributes:context:

It now says I should be using -boundingRectWithSize:options:attributes:context:

此方法对iOS7来说并不是新方法,因此我认为可以在堆栈溢出时询问它,而不是去参加正式的Apple开发者论坛.

This method isn't new to iOS7 and therefore i figure that it is okay to ask it on stack overflow, rather than going across to the official apple developers forum.

我有三个问题:

1)因为它已被弃用,这是否意味着即使它仍然可以工作,我还是应该将其替换吗?

1) Because it is deprecated, does that mean I should definitely replace it, despite it still working?

2)我尝试了许多不同的boundingRectWithSize:方法,这些方法具有各种变量,但是它从来都不是完美的,它似乎总是略有不足(正如许多stackoverflow问题所指出的那样)-是否可以用这种不被弃用的方法来完美替代与我以前的方法完全一样,麻烦最少吗?

2) I have tried many different boundingRectWithSize: methods, with various variables but it is never perfect, it always seems to be slightly out (as many stackoverflow questions point out) - is there a perfect replacement with this none-deprecated method that does exactly the same as my previous method with as minimal hassle?

3)为什么要删除此方法?是因为与其他方法重叠吗?

3) why remove this method? Is it because of the overlap with this other method?

推荐答案

一个小时的试用错误后,我设法使其正常工作:

After an hour of trial error I managed to make it work:

CGSize maximumLabelSize = CGSizeMake(tableView.width, MAXFLOAT);

NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
                                 NSStringDrawingUsesLineFragmentOrigin;

NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
CGRect labelBounds = [string boundingRectWithSize:maximumLabelSize 
                                          options:options
                                       attributes:attr
                                          context:nil];

更新:

正如T先生在下面的回答中提到的:在iOS 7及更高版本中,此方法返回小数大小(在返回的CGRect的大小部分中);要使用返回的大小来调整视图的大小,必须使用ceil函数将其值提高到最接近的较大整数. ceilf建议使用.

As Mr. T mentions in answer below : In iOS 7 and later, this method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function. ceilf function is recommended to use.

CGFloat height = ceilf(labelBounds.size.height);

这篇关于我应该用什么替换不推荐使用的sizeWithFont:方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆