gpt4 book ai didi

使用核心文本框架的 PDF 生成中的 IOS 文本颜色不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:13 25 4
gpt4 key购买 nike

我正在使用以下代码在 Pdf 生成中设置文本颜色。代码运行良好,唯一的问题是它没有设置文本颜色。

   -(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect fontSize:
(float)sizeofFont
{
NSLog(@"FONT SIZE %f",sizeofFont);
int length=[textToDraw length];
CFStringRef string = (__bridge CFStringRef) textToDraw;

CFMutableAttributedStringRef currentText =
CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string);
CTFontRef font = CTFontCreateWithName((CFStringRef)@"UniversLTStd-BoldCn",
sizeofFont, nil);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0,
length),kCTFontAttributeName,font);

CGColorSpaceCreateWithName(string);

NSMutableAttributedString *string11 = [[NSMutableAttributedString alloc]
initWithString:textToDraw];

[string11 addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSMakeRange(0, [string11 length])];


CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(currentText);

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(currentText);
CFRelease(framesetter);
//CFRelease(stringRef);
}

但是文字颜色没有改变。请帮我看看怎么办?我有不同的标签,我必须为不同的标签设置不同的颜色。

最佳答案

尝试替换这段代码:

CGColorSpaceCreateWithName(string);

NSMutableAttributedString *string11 = [[NSMutableAttributedString alloc]
initWithString:textToDraw];

[string11 addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSMakeRange(0, [string11 length])];

与:

CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,[UIColor redColor].CGColor); 

关于使用核心文本框架的 PDF 生成中的 IOS 文本颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22005921/

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