2013-12-23 27 views
0

以前在我的應用程序中,我使用的是這樣的sizeWithFont在iOS 7中使用sizeWithAtrributes時,方法調用錯誤的參數太多iPhone

我試圖根據文本標籤的寬度和位置調整圖像的大小和位置。所以我嘗試我喜歡這個,

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」 cordinate。

After deprecation

任何人都可以請幫我在這。

回答

1

替換此代碼:

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]}]; 

看起來它只是一個,這樣它不會工作。

+0

謝謝。我試過這種方式CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:@ {NSFontAttributeName:[UIFont systemFontOfSize:[self.categoryLabel.frame.size.width] + 10,y,_imageSize。寬度,_imageSize.height]}];現在它顯示「Expected identifier」錯誤 – suji

+0

你可以看看我編輯的答案 – suji

+1

@suji我編輯我的帖子,這是我的代碼中的錯誤。假設你想獲得sizeWithFont:方法的寬度,但說實話我不知道你想達到什麼目的。你的問題是如何解決一個錯誤,編輯後的代碼應該這樣做。 – Greg

相關問題