gpt4 book ai didi

iphone - 如何使用核心文本为PDF设置字体颜色和样式?

转载 作者:行者123 更新时间:2023-11-28 20:17:29 25 4
gpt4 key购买 nike

如果我使用 core text 框架创建 PDF,它将不允许我设置颜色和字体样式。“核心文本”框架有问题吗?

我使用下面的代码来设置颜色:

CGContextRef    currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 1.0, 0.0, 0.0, 1.0); //This is not working.

最佳答案

试试下面的代码,在这段代码中我用 CTFontRef 改变了我的字体样式和大小

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{
CFStringRef stringRef = ( CFStringRef)textToDraw;

CGColorSpaceCreateWithName(stringRef);

NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:textToDraw];
CTFontRef helveticaBold;
helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 24.0, NULL);
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, [string length])];
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor whiteColor].CGColor
range:NSMakeRange(0, [string length])];

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);

CGMutablePathRef framePath = CGPathCreateMutable();

CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);

CGContextScaleCTM(currentContext, 1.0, -1.0);

CTFrameDraw(frameRef, currentContext);

CGContextScaleCTM(currentContext, 1.0, -1.0);

CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);

CFRelease(frameRef);
CFRelease(string);
CFRelease(framesetter);
}

您也可以点击此链接 Example

希望这对你有帮助,祝你编程愉快

关于iphone - 如何使用核心文本为PDF设置字体颜色和样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441007/

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