gpt4 book ai didi

ios - 生成PDF时不出现NSAttributedString

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:24 26 4
gpt4 key购买 nike

这曾经有效,现在突然停止了:

对于 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 中的一个错误,我决定使用 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];
}

关于ios - 生成PDF时不出现NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com