我正在使用以下代码来设置 Pdf 生成中的文本颜色。代码运行良好,唯一的问题是它没有设置文本颜色。
-(void)drawTextNSString*)textToDraw inFrameCGRect)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 addAttributeid)kCTForegroundColorAttributeName
valueid)[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 addAttributeid)kCTForegroundColorAttributeName
valueid)[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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |