Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTParagraphStyleSetting + CoreText: Setting line spacing

I'm trying to set the line spacing of NSAttributedString. The problem is that I can't set a small linespacing for the text. I can set huge values for the leading, like 25 or more, but small values are ignored. I'm trying to set kCTParagraphStyleSpecifierMinimumLineHeight or kCTParagraphStyleSpecifierMinimumLineSpacing, but it is not working.

This is my code, any help is appreciated.

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;
like image 428
jberlana Avatar asked May 31 '26 06:05

jberlana


1 Answers

You also need to set the maximum line spacing.

like image 95
Cocoanetics Avatar answered Jun 01 '26 19:06

Cocoanetics