使用核心文本框架生成 PDF 中的 IOS 文本颜色不起作用
<p><p>我正在使用以下代码来设置 Pdf 生成中的文本颜色。代码运行良好,唯一的问题是它没有设置文本颜色。</p>
<pre><code> -(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect fontSize:
(float)sizeofFont
{
NSLog(@"FONT SIZE %f",sizeofFont);
int 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 = [
initWithString:textToDraw];
[string11 addAttribute:(id)kCTForegroundColorAttributeName
value:(id).CGColor
range:NSMakeRange(0, )];
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);
}
</code></pre>
<p>但是文字颜色没有改变。请帮我怎么做?
我有不同的标签,我必须为不同的标签设置不同的颜色。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试替换此代码:</p>
<pre><code>CGColorSpaceCreateWithName(string);
NSMutableAttributedString *string11 = [
initWithString:textToDraw];
[string11 addAttribute:(id)kCTForegroundColorAttributeName
value:(id).CGColor
range:NSMakeRange(0, )];
</code></pre>
<p>与:</p>
<pre><code>CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTForegroundColorAttributeName,.CGColor);
</code></pre></p>
<p style="font-size: 20px;">关于使用核心文本框架生成 PDF 中的 IOS 文本颜色不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22005921/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22005921/
</a>
</p>
页:
[1]