ios - 在 ios 7 iPhone 中使用 sizeWithAtrributes 时,方法调用参数过多错误

标签 ios iphone objective-c ios7 uifont

之前在我的应用程序中我使用 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”坐标。 Before deprecating

但是当我使用这样的代码时 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”坐标是这样的。

After deprecation

任何人都可以帮我解决这个问题吗?

最佳答案

替换这段代码:

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,_im‌​ageSize.height]}];

看起来它只是一个所以它不会工作。

关于ios - 在 ios 7 iPhone 中使用 sizeWithAtrributes 时,方法调用参数过多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747821/

相关文章:

ios - 相同大小级别的不同设备上的不同字体大小

iphone - 以编程方式安装 .mobileconfig

iphone - 如何获取 iPhone 地址簿联系人电子邮件作为 NSStrings?

iphone - FBLogin 按钮调整大小

ios - 如何将数据添加到 CollectionView 中,仅用于数组 Swift 中的新附加索引

iphone - EGOTableViewPullRefresh - 由于链接问题无法构建

ios - 在 Swift 应用程序中安装 Realm

ios - 具有自定义图像且无边框的 UIBarButtonItem

iphone - 如何通过代码将UISearchBar设置为黑色半透明?

iphone - 如何绘制只是一个圆圈的自定义 UIView - iPhone 应用程序