2012-01-09 3 views
0

私はラインあたりCTFrameRefのみ8文字で描くしようとしているが、私はCTFrameRefでこれを行う方法を知りません。CTFrameRefで1行に8文字しか描画できないのですか?

私は "Manual Line Breaking"と読みましたが、CTLineを使用しています。私はCTFrameRefを使う方が簡単だと思います。

多分フレームセッターやCGPathを変更することで可能かもしれません。

これが今の私のコードです(私はこの問題についての私のテストコードとコメント行を削除した):

// Prepare font 
    CTFontRef font = CTFontCreateWithName(CFSTR("LucidaSansUnicode"), 16, NULL); 

// Create path   
    CGMutablePathRef gpath = CGPathCreateMutable(); 
    CGPathAddRect(gpath, NULL, CGRectMake(0, 0, 200, 200)); 

// Create an attributed string 
    CGContextSetTextDrawingMode (newcontext, kCGTextFillStroke); 
    CGContextSetGrayFillColor(newcontext, 0.0, 1.0); 

CFStringRef keys[] = { kCTFontAttributeName }; 
CFTypeRef values[] = { font }; 
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, 
    (const void **)&values, sizeof(keys)/sizeof(keys[0]), 
    &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, 
    CFSTR("Aenean lacinia bibendum nulla sed consectetur."), attr); 

CTFramesetterRef framesetter = 
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString); 

CTFrameRef theFrame = 
    CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 
    CFAttributedStringGetLength(attrString)), gpath, NULL); 

CTFrameDraw(theFrame, newcontext); 

// Clean up... CFRelease... 

// Getting TIFF image 

私は、次の画像を取得する:

Wrong Image

しかし、私このようなものが欲しいです(この画像はPhotoshopで作成されているため、違いがあります)。

ありがとう、ごめんなさい。

答えて

2

なぜCTFrameは、CTLineよりもこの種の問題の方が簡単だと思いますか?あなたは、珍しい行分割アルゴリズムが必要です。 CTFrameはデフォルトの改行アルゴリズムを使用します。

入力を8文字のNSAttributedStringの実行に分割し、CTLineRefオブジェクトを作成してから描画します。これは非常に簡単です。混乱の原因は何ですか?