【问题标题】:How can I draw only 8 character per line in a CTFrameRef?如何在 CTFrameRef 中每行仅绘制 8 个字符?
【发布时间】:2012-01-09 02:19:22
【问题描述】:

我正在尝试在 CTFrameRef 中仅绘制 每行 8 个字符,但我不知道如何使用 CTFrameRef .

我读过“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

我得到以下图片:

但我想要这样的东西(有区别,因为这张图片是用 Photoshop 制作的):

感谢和抱歉我的英语。

【问题讨论】:

    标签: macos cocoa drawing core-graphics core-text


    【解决方案1】:

    为什么你认为CTFrameCTLine更容易解决这类问题?你想要一个不寻常的换行算法。 CTFrame 使用默认的换行算法。

    将您的输入分解为 8 个字符的 NSAttributedString 运行并使用它们创建 CTLineRef 对象,然后绘制它们。这应该非常简单。造成混乱的原因是什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 2021-04-28
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      相关资源
      最近更新 更多