2014-01-14 5 views
2

Это используется для работы, и теперь, внезапно, он перестал работать:NSAttributedString не появляется при создании PDF

Для IOS 7 IPad App, сгенерировать PDF с

UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil); 
... 
UIGraphicsEndPDFContext(); 

Within что блок кода, следующие методы, используемые для визуализации текста подчеркнуты, но в последнее время, он просто перестал работать, и любой текст передается метода не отображается на всех:

+(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int)locationX andLocationY:(int)locationY andTextAreaWidth:(int)textWidth andTextAreaHeight:(int)textHeight{ 

    NSDictionary *attributesDict; 
    NSMutableAttributedString *attString; 

    attributesDict = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle]}; 
    attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, color.CGColor); 
    CGRect rect = CGRectMake(locationX, locationY, textWidth, textHeight); 
    [attString drawInRect:rect]; 
} 

Я не могу найти ничего на ш eb в отношении рисования в контексте PDF. Есть posts (and here), в котором упоминается эта проблема в отношении меток. Но, как представляется, решение для моей проблемы при создании файлов PDF ...

Пожалуйста, помогите!

+2

Это ошибка в прошивке 7. Я столкнулся с той же проблемой. Если атрибутная строка имеет любой подчеркнутый текст, она не будет отображаться в PDF-файле. Я представил отчет об ошибке 2 месяца назад. – rmaddy

+2

Мое обходное решение заключалось в том, чтобы удалить любую подчеркивание, чтобы при остальном текст появлялся. – rmaddy

+0

ОК, @rmaddy Я также представил отчет. – matrix4use

ответ

2

Поскольку это ошибка в прошивке я решил использовать обходные из here, просто рисуя линию к контексту (см комментариев в коде):

+(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int)locationX andLocationY:(int)locationY andTextAreaWidth:(int)textWidth andTextAreaHeight:(int)textHeight{ 

    NSDictionary *attributesDict; 
    NSMutableAttributedString *attString; 

    // Commented out until iOS bug is resolved:  
    //attributesDict = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle], NSForegroundColorAttributeName : color, NSFontAttributeName : font}; 
    attributesDict = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font}; 
    attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, color.CGColor); 
    CGRect rect = CGRectMake(locationX, locationY, textWidth, textHeight); 

    // Temporary Solution to NSUnderlineStyleAttributeName - Bug: 
    CGContextSetStrokeColorWithColor(context, color.CGColor); 
    CGContextSetLineWidth(context, 1.0f); 

    CGSize tmpSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999)]; 

    CGContextMoveToPoint(context, locationX, locationY + tmpSize.height - 1); 
    CGContextAddLineToPoint(context, locationX + tmpSize.width, locationY + tmpSize.height - 1); 

    CGContextStrokePath(context); 
    // End Temporary Solution 

    [attString drawInRect:rect]; 
} 
Смежные вопросы