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

标签 ios

我正在使用以下代码在 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/

相关文章:

c# - MonoTouch 对话框部分中的纯文本?

iphone - 可以限制 UIScrollView 中的缩放/平移区域吗?

ios - 如何按我当前的位置过滤 Google Places 自动完成结果? swift

ios - 部署后向 App-Info.plist 添加值

iphone - 这段代码中dispatch_after方法是并发执行的吗?

ios - 如何以编程方式从类加载 Storyboard?

ios - 使用获取的属性进行核心数据跨存储查询

ios - swift 将 NSDictionary 分配给全局 NSMUtableDictionary

ios - 禁用 CloudKit 调试日志

ios - uipageviewcontroller 内的 uiwebview 阻止点击以翻页