6

drawpdf関数を使用してiosアプリでpdfを生成しています.nsobjectクラスでdrawtext関数を呼び出しながら、指定したフレームと文字列に従ってテキストを明確に描画します。

私のコードは

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{

    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);


    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

// Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

// Get the graphics context.
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);


// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);


    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);
}

しかし、テキストのフォントサイズを大きく太字に設定する必要があります。

使った

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman);

CGContextSetFontSize ( currentContext, 20);

上記の関数で mr.texian によって提供された回答を参照しますが、変更はありません

生成された pdf を uiwebview で表示しています。

このコードはネットから取得しました。フォント設定のコードをどこで編集すればよいかわかりませんでした。どんな助けでも大歓迎です。

4

6 に答える 6

6

非常に便利な基礎クラスである CoreText をお試しください。サンプルコードは次のとおりです。

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Arial", 16.0f, nil);
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font);

CFAttributtedStringRefこれをポインタに追加します。

これはうまくいくはずです。

于 2013-01-30T11:33:53.977 に答える
2

CTFontを使用して作成しますCTFontCreateWithNameAndOptions(チェック:CTFont Reference

変更可能な属性文字列を作成する ( CFMutableAttributedString リファレンス) この変更可能な文字列を編集して、必要な変更を加えることができます。あなたの場合、フォントを設定します。

この文字列を使用して CTFrameSetter を作成すると、フォントが表示されます。

十分な時間がある場合は、String プログラミング ガイドをお読みください。

CFMutableAttributedString を使用するための簡単なコード サンプルが必要な場合は、stackoverflow で CFMutableAttributedString を検索してください。ほとんどの回答で、これを使用する方法がわかります。たとえば、この質問について

于 2013-01-24T11:39:13.933 に答える
2

これらはあなたが探しているものだと思います

void CGContextSelectFont (
    CGContextRef c,
    const char *name,
    CGFloat size,
    CGTextEncoding textEncoding
);

void CGContextSetFontSize (
   CGContextRef c,
   CGFloat size
);

何かのようなもの...

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman);

また

CGContextSetFontSize ( currentContext, 20);

お役に立てば幸いです。詳細はこちら... Quartz 2D プログラミング ガイド - テキスト

于 2013-01-21T15:57:06.423 に答える
1

どうCGContextSetFontですか?

NSString *fontName = @"Helvetica";
CGFontRef fontRef = CGFontCreateWithFontName((__bridge CFStringRef)fontName);
CGContextSetFont(context, fontRef);
CGContextSetFontSize(context, 20);

それを解放することを忘れないでください:

CGFontRelease(fontRef);
于 2013-01-30T16:23:17.290 に答える
0

多くの検索の後、そのdamslifeの答えは正しいことがわかりましたが、それは私のコードをサポートしていませんでした.

でも、安く解決しました。つまり、drawText:太字フォントを取得する関数を複数回呼び出します。

for(int i=0;i<5;i++)
{
 [self drawtext];
} 
于 2013-02-18T09:39:15.617 に答える