在CTFrame中设置行距 [英] Setting line spacing in a CTFrame

查看:135
本文介绍了在CTFrame中设置行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种QR Code生成器,它是Mac应用程序. 我知道如何绘制文本,但是我不知道如何设置行距.

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.

我的代码:

// 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);

我一直在搜索,但没有找到任何东西.也许我必须使用CTLine吗?

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

更新:

好的,我发现了这一点,但并不能解决我的问题,我希望行间距小于0.0点.

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);

推荐答案

尝试使用kCTParagraphStyleSpecifierLineHeightMultiple而不是kCTParagraphStyleSpecifierLineSpacingAdjustment.那对我有用. 请注意,1.0行距是1.2 * yourLineHeight.

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

这篇关于在CTFrame中设置行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆