【问题标题】:NSAttributedString does not appear when generating PDF生成PDF时不出现NSAttributedString
【发布时间】:2014-02-01 22:57:15
【问题描述】:

这曾经可以工作,现在突然停止工作:

对于 iOS 7 iPad 应用,我使用

生成 PDF
UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil);
...
UIGraphicsEndPDFContext();

在该代码块中,以下方法用于呈现带下划线的文本,但最近,它刚刚停止工作,并且根本不呈现传递给该方法的任何文本:

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

我在网上找不到任何关于绘制到 PDF 上下文的内容。有posts (and here) 提到了与标签有关的问题。但是生成PDF文件时似乎没有解决我的问题...

请帮忙!

【问题讨论】:

  • 这是 iOS 7 中的一个错误。我遇到了同样的问题。如果属性字符串有任何带下划线的文本,它将不会出现在 PDF 中。我在 2 个月前提交了一份错误报告。
  • 我的解决方法是删除所有下划线,以便至少显示其余文本。
  • 好的,@rmaddy 我也提交了报告。

标签: ios pdf nsattributedstring underline


【解决方案1】:

由于这是 iOS 中的一个错误,我决定使用here 中的解决方法,只需在上下文中画一条线(参见代码中的 cmets):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多