2014-02-22 32 views
0

我想在我的演示項目中加入一個爲iOS 7編寫的示例代碼。我收到了一些與drawAtPointsizeWithFont相關的折舊方法。我已經通讀了一堆有相同問題的SO條目,但它們都與標籤和tableview單元格等有關。我正在嘗試繪製PDF到我的視圖中,並且不知何故我無法拿出正確的代碼集用新方法。我真的很感謝任何幫助人。這裏是我使用的類的部分:如何解決與sizeWithFont和drawAtPoint相關的折舊警告?

UIFont* studentNameFont = [UIFont boldSystemFontOfSize:17]; 
UIFont* classFont = [UIFont systemFontOfSize:15]; 

CGFloat currentPageY = 0; 

for (NSDictionary* student in students) 
{ 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); 
    currentPageY = kMargin; 

    NSString* name = [NSString stringWithFormat:@"%@ %@", 
         [student objectForKey:@"FirstName"], 
         [student objectForKey:@"LastName"]]; 

    // depreciated method 
    CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:NSLineBreakByWordWrapping]; 

    //depreciated method 
    [name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:NSLineBreakByWordWrapping]; 
    currentPageY += size.height; 

    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]); 
    CGContextMoveToPoint(context, kMargin, currentPageY); 
    CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY); 
    CGContextStrokePath(context); 

    NSArray* classes = [student objectForKey:@"Classes"]; 
    for(NSDictionary* class in classes) 
    { 
     NSString* className = [class objectForKey:@"Name"]; 
     NSString* grade = [class objectForKey:@"Grade"]; 

    //depreciated method 
     size = [className sizeWithFont:classFont constrainedToSize:CGSizeMake(classNameMaxWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; 

     // if the current text would render beyond the bounds of the page, 
     // start a new page and render it there instead 
     if (size.height + currentPageY > maxHeight) { 
      // create a new page and reset the current page's Y value 
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); 
      currentPageY = kMargin; 
     } 
     //depreciated method 
     [className drawInRect:CGRectMake(kMargin, currentPageY, classNameMaxWidth, maxHeight) withFont:classFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft]; 

     //depreciated method 
     [grade drawInRect:CGRectMake(kMargin + classNameMaxWidth + kColumnMargin, currentPageY, gradeMaxWidth, maxHeight) withFont:classFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft]; 

     currentPageY += size.height; 

    } 
+0

爲僅適用於iOS 7的開發? –

+0

然後你有答案咆哮由C_X,併爲drawAtPoint http://stackoverflow.com/questions/17535974/nsstring-drawatpoint-with –

+0

您通過閱讀棄用方法的文檔,並使用引用的新方法修復棄用問題在那些文檔中。 – rmaddy

回答

1

您可以使用此方法

[className sizeWithAttributes:@{NSFontAttributeName:classFont}]; 
+0

接受了你的回答朋友。如果你有機會可以幫助其他警告? –