有人知道如何使用-boundingRectWithSize:options:attributes:context:来替代本例中不推荐使用的“sizeWithFont:constrainedToSize:”方法。
CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];获取警告:“sizeWithFont:constrainedToSize:”已弃用:在iOS 7.0中首次被弃用-请使用-boundingRectWithSize:options:attributes:context:
这是洞的代码片段:
// calculate the label size
CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];
each_object(self.labels, ^(UILabel *label) {
CGRect frame = label.frame;
frame.origin.x = offset;
frame.size.height = CGRectGetHeight(self.bounds);
frame.size.width = labelSize.width + 2.f /*Magic number*/;
label.frame = frame;
// Recenter label vertically within the scroll view
label.center = CGPointMake(label.center.x, roundf(self.center.y - CGRectGetMinY(self.frame)));
offset += CGRectGetWidth(label.bounds) + self.labelSpacing;
});发布于 2014-02-27 19:38:00
目前你有..。
CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];所以使用...
CGRect boundingRect = [self.mainLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize labelSize = boundingRect.size;这应该行得通。
或者..。有了属性...
CGRect boundingRect = [self.mainLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.mainLabel.font}
context:nil];发布于 2014-02-27 19:46:09
例如,这样做
-(CGFloat)getLabelSize:(UILabel *)label fontSize:(NSInteger)fontSize
{
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:fontSize], NSFontAttributeName,
nil];
CGRect frame = [label.text boundingRectWithSize:CGSizeMake(270, 2000.0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
CGSize size = frame.size;
return size.height;
}https://stackoverflow.com/questions/22067284
复制相似问题