在ios7中,sizeWithFont:现在已弃用。我现在如何在UIFont对象传递到替换方法sizeWithAttributes:?
当前回答
替代解决方案,
CGSize expectedLabelSize;
if ([subTitle respondsToSelector:@selector(sizeWithAttributes:)])
{
expectedLabelSize = [subTitle sizeWithAttributes:@{NSFontAttributeName:subTitleLabel.font}];
}else{
expectedLabelSize = [subTitle sizeWithFont:subTitleLabel.font constrainedToSize:subTitleLabel.frame.size lineBreakMode:NSLineBreakByWordWrapping];
}
其他回答
替代解决方案,
CGSize expectedLabelSize;
if ([subTitle respondsToSelector:@selector(sizeWithAttributes:)])
{
expectedLabelSize = [subTitle sizeWithAttributes:@{NSFontAttributeName:subTitleLabel.font}];
}else{
expectedLabelSize = [subTitle sizeWithFont:subTitleLabel.font constrainedToSize:subTitleLabel.frame.size lineBreakMode:NSLineBreakByWordWrapping];
}
// max size constraint
CGSize maximumLabelSize = CGSizeMake(184, FLT_MAX)
// font
UIFont *font = [UIFont fontWithName:TRADE_GOTHIC_REGULAR size:20.0f];
// set paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
// dictionary of attributes
NSDictionary *attributes = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle.copy};
CGRect textRect = [string boundingRectWithSize: maximumLabelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGSize expectedLabelSize = CGSizeMake(ceil(textRect.size.width), ceil(textRect.size.height));
最好使用自动尺寸(Swift):
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
注: 1. UITableViewCell原型应该正确设计(对于实例不要忘记设置UILabel。numberolines = 0 etc) 2. 删除hightforrowatindexpath方法
视频: https://youtu.be/Sz3XfCsSb6k
I believe the function was deprecated because that series of NSString+UIKit functions (sizewithFont:..., etc) were based on the UIStringDrawing library, which wasn't thread safe. If you tried to run them not on the main thread (like any other UIKit functionality), you'll get unpredictable behaviors. In particular, if you ran the function on multiple threads simultaneously, it'll probably crash your app. This is why in iOS 6, they introduced a the boundingRectWithSize:... method for NSAttributedString. This was built on top of the NSStringDrawing libraries and is thread safe.
如果你看一下新的NSString boundingRectWithSize:…在NSAttributeString函数中,它以与NSAttributeString相同的方式请求一个属性数组。如果我不得不猜测,ios7中的这个新的NSString函数仅仅是ios6中NSAttributeString函数的包装。
在这一点上,如果你只支持iOS 6和iOS 7,那么我肯定会改变你所有的NSString sizeWithFont:…NSAttributeString boundingRectWithSize。如果您恰好有一个奇怪的多线程极端情况,它将为您省去很多麻烦!下面是我如何转换NSString sizeWithFont:constrainedToSize::
过去是什么样子:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
CGSize size = [text sizeWithFont:font
constrainedToSize:(CGSize){width, CGFLOAT_MAX}];
可替换为:
NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:text
attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
请注意文件中提到:
在iOS 7及更高版本中,此方法返回分数大小(在size . size中) 返回的CGRect的组件);要使用返回的大小到大小 视图中,必须使用将其值提高到最近的更高整数 使用cell函数。
因此,要抽出计算的高度或宽度,以用于调整视图,我将使用:
CGFloat height = ceilf(size.height);
CGFloat width = ceilf(size.width);
- (CGSize) sizeWithMyFont:(UIFont *)fontToUse
{
if ([self respondsToSelector:@selector(sizeWithAttributes:)])
{
NSDictionary* attribs = @{NSFontAttributeName:fontToUse};
return ([self sizeWithAttributes:attribs]);
}
return ([self sizeWithFont:fontToUse]);
}
推荐文章
- Objective-C中方法混合的危险是什么?
- 如何使用接口生成器创建的nib文件加载UIView
- 如何设置回退按钮文本在Swift
- 如何比较两个nsdate:哪个是最近的?
- 使UINavigationBar透明
- Swift to Objective-C头未在Xcode 6中创建
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded
- 不区分大小写的比较
- KeyboardEvent。键码弃用。这在实践中意味着什么?
- 编译警告:对于架构i386,没有处理文件的规则
- 从ViewController调用App Delegate方法
- 解释iOS7中automyadjustsscrollviewinsets, extendedlayoutinclesopaquebars, edgesForExtendedLayout之间的区别
- 你能把一个UIGestureRecognizer附加到多个视图吗?
- Pod安装保持在“设置CocoaPods主repo”状态
- 从路径字符串中提取文件名