之前在我的应用程序中我使用 sizeWithFont
像这样。
我正在尝试根据文本标签的宽度和位置调整图像的大小和位置。所以我尝试我喜欢这个,
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithFont:self.categoryLabel.font].width + 10, y, _imageSize.width, _imageSize.height);
但在 iOS 7 中,它已被弃用并要求使用 sizeWithAttributes。
当我这样尝试时,我得到了这样的“x”坐标。
但是当我使用这样的代码时 CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.frame.size.width+10]}],y,_imageSize.width,_imageSize.height);
我得到的“x”坐标是这样的。
任何人都可以帮我解决这个问题吗?
最佳答案
替换这段代码:
self.categoryLabel.font.width+10
与
self.categoryLabel.frame.size.width+10
字体没有宽度属性。我假设您想从标签的框架中获取宽度。
//扩展
这是整个矩形创建行的示例,CGRect 需要 4 个参数,我逐行列出它,用逗号分隔,我不确定它是否是您需要的,但它会给您一些结果:
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.font.pointSize]}].width, //<- this .width takes width ot the size with font but maybe you required height
y,
_imageSize.width,
_imageSize.height);
如果您查看下面的行(您的旧行),则没有 4 个参数:
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[self.categoryLabel.frame.size.width]+10,y,_imageSize.width,_imageSize.height]}];
看起来它只是一个所以它不会工作。
关于ios - 在 ios 7 iPhone 中使用 sizeWithAtrributes 时,方法调用参数过多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747821/