CTParagraphStyleSetting + CoreText:设置行间距

时间:2013-01-10 11:25:45

标签: ios core-text nsattributedstring

我正在尝试设置NSAttributedString的行间距。问题是我无法为文本设置小的行间距。我可以设置领先的巨大值,如25或更多,但忽略小值。我正在尝试设置kCTParagraphStyleSpecifierMinimumLineHeightkCTParagraphStyleSpecifierMinimumLineSpacing,但它无效。

这是我的代码,感谢任何帮助。

CGFloat leading = 50.0;
CGFloat minMineHeight = 1;
CTTextAlignment alignment = kCTCenterTextAlignment;

const CTParagraphStyleSetting styleSettings[] = {

    {kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(CGFloat), &minMineHeight},
    {kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &minMineHeight},
    {kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &leading},
    {kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &leading},
    {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment}
};

CTParagraphStyleRef style = CTParagraphStyleCreate((const CTParagraphStyleSetting*) &styleSettings, 5);
[mutableAttributedString addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(__bridge id)style range:NSMakeRange(0, [mutableAttributedString length])];
CFRelease(style);

return mutableAttributedString;

1 个答案:

答案 0 :(得分:1)

您还需要设置最大行间距。