2012-01-09 17 views
0

我想在CTFrameRef繪製每行隻字符,但我不知道如何與CTFrameRef做到這一點。如何在CTFrameRef中每行只繪製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製作):

Wanted Image

感謝和抱歉我的英語。

回答

2

爲什麼您認爲CTFrameCTLine更容易出現此類問題?你想要一個不尋常的換行算法。 CTFrame使用默認的換行算法。

把你的輸入分成8個字符NSAttributedString運行並創建CTLineRef對象,然後繪製它們。這應該是非常直截了當的。什麼導致了混亂?

相關問題