2013-10-25 97 views
3

我似乎無法正確替換棄用的sizeWithFontboundingRecWithSize。我搜遍了所有的答案,並徹夜試圖解決這個問題。我真的需要某個人比我聰明的方式提供幫助。下面是我正在修改的代碼。任何幫助,將不勝感激。IOS 7 sizeWithFont已棄用

CGSize sizeForText = [faqItem.answer sizeWithFont:[UIFont boldSystemFontOfSize:14] 
    constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT) 
    lineBreakMode:NSLineBreakByWordWrapping]; 

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)] 
    inRowHeightsAtIndex:0]; 
+1

可能重複的[置換棄用sizeWithFont:在IOS 7](http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7) –

回答

-3

在蘋果documentation

sizeWithFont:返回字符串的大小,如果它是要被渲染 與在單獨的一行指定的字體。 (不贊成。在IOS 7.0使用 sizeWithAttributes:不是。)

  • (CGSize)sizeWithFont:(UIFont *)字體參數要使用的字體計算字符串大小的字體。返回值 生成的字符串的邊界框的寬度和高度。這些值可以四捨五入爲最接近的整數 。

所以,你可以使用sizeWithAttributes:這樣的:

CGSize sizeForText = [faqItem.answer sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} 
         constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT) 
          lineBreakMode:NSLineBreakByWordWrapping]; 

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)] 
     inRowHeightsAtIndex:0]; 
9

您需要使用sizeWithAttributes屬性。

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f]}]; 

你也可以將它以減少重新編碼,如果你使用的大小不止一次設置爲一個已創建的字體大小:

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: label1.font}]; 

我不相信你可以使用constrainedToSize與這個屬性。它必須在CGRect上單獨設置。

2

我給你寫了一個樣本,希望對你有幫助。的

NSString *text = @" // Do any additional setup after loading the view, typically from a nib."; 
CGRect rect = CGRectZero; 
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:17]}; 

rect = [text boundingRectWithSize:CGSizeMake(100,9999) 
          options:(NSStringDrawingUsesLineFragmentOrigin) 
         attributes:attrDict 
          context:Nil]; 

UILabel *lbl = [[UILabel alloc] init]; 
lbl.text = text; 
rect.origin = CGPointMake(50, 200); 
lbl.frame = rect; 
lbl.lineBreakMode = NSLineBreakByWordWrapping; 
lbl.numberOfLines = 0; 
[self.view addSubview:lbl]; 
lbl.backgroundColor = [UIColor lightGrayColor];