NSAttributedString kCTParagraphStyleSpecifierParagraphSpacing not affected

When applying the kCTParagraphStyleSpecifierParagraphSpacing style, it does not have a visual effect on the rendered text. Other attributes, such as line spacing and text alignment, work just fine. What can i do wrong?

CTTextAlignment theAlignment = kCTRightTextAlignment; CGFloat paragraphSpacingFloat = 150.0; CGFloat paragraphSpacingBeforeFloat = 150.0; CGFloat lineSpacing = CTFontGetLeading(baseFont)*5.0; CFIndex theNumberOfSettings = 4; CTParagraphStyleSetting theSettings[4] = { { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &paragraphSpacingFloat }, { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBeforeFloat }, { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment }, { kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing } }; CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings); [attr addAttribute:(id)kCTParagraphStyleAttributeName value:(id)theParagraphRef range:r]; [attr addAttribute:(id)kCTFontAttributeName value:(id)baseFont range:r]; CFRelease(theParagraphRef); 

I draw text using

 CTFrameSetter frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attr); CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake([[attr string] length], 0), the_drawing_cgrect, NULL); CTFrameDraw(frame, context); 
+6
source share
2 answers

Are you sure your line contains paragraph separators? kCTParagraphStyleSpecifierParagraphSpacing does not work on newlines. This requires the actual.

Try this code to replace all newlines with paragraph separators:

 NSArray *paragraphs = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; NSString *text = [items componentsJoinedByString:@"\u2029"]; 
+4
source

Source: https://habr.com/ru/post/885936/


All Articles