Setting line spacing in a CTFrame

删除回忆录丶 提交于 2019-12-13 02:35:38

问题


I'm working in a kind of QR Code generator, it's a Mac app. I know how draw the text, but I don't know how I can set line spacing.

My code:

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

    // Create Path
    CGMutablePathRef gpath = CGPathCreateMutable();
    CGPathAddRect(gpath, NULL, CGRectMake(10, 0, pixelsWidth, pixelsHigh));

    CGContextSetTextDrawingMode (newcontext, kCGTextFill); 
    CGContextSetGrayFillColor(newcontext, 0.0, 1.0);

    // Create an attributed string
    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, stringCode, attr);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);

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

    // Draw the string
    CTFrameDraw(theFrame, newcontext);

I have been searching but I don't find anything. Maybe must I use CTLine?

Update:

Okay, I found this but it don't solve my problem, I want less than 0.0point of line spacing.

CTTextAlignment paragraphAlignment = kCTLeftTextAlignment;

    CGFloat maxLine = 0.0;

    CTParagraphStyleSetting setting[2] = {
        {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &paragraphAlignment},
        {kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &maxLine}
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 2);

回答1:


Try to use kCTParagraphStyleSpecifierLineHeightMultiple instead of kCTParagraphStyleSpecifierLineSpacingAdjustment. That worked for me. Note, that 1.0 line spacing is 1.2 * yourLineHeight.



来源:https://stackoverflow.com/questions/9017593/setting-line-spacing-in-a-ctframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!