在 CTFrame 中设置行距

发布于 2024-12-28 20:24:30 字数 1805 浏览 1 评论 0原文

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

我的代码:

// 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?

更新:

好的,我发现了这个,但它不能解决我的问题,我想要小于 0.0 点的行间距。

CTTextAlignment paragraphAlignment = kCTLeftTextAlignment;

    CGFloat maxLine = 0.0;

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

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 2);

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), ¶graphAlignment},
        {kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &maxLine}
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 2);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-01-04 20:24:30

尝试使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文