0

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

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

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

2 回答 2

3

试试下面的代码,在这段代码中,我用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);
}

您也可以按照此链接进行 操作示例

希望这对你有帮助,快乐编码

于 2013-07-03T07:08:30.283 回答
0

您可以像这样创建颜色对象:

UIColor *colour = [UIColor colorWithRed:0 green:1 blue:0 alpha:0];

并填充颜色:

CGContextSetFillColorWithColor(ctx, colour.CGColor);

如果您不使用 ARC,则需要适当地保留和释放颜色。

于 2013-09-20T03:15:01.240 回答