开发者

NSAttributedString kCTParagraphStyleSpecifierParagraphSpacing has no effect

开发者 https://www.devze.com 2023-02-25 02:53 出处:网络
When applying the kCTParagraphStyleSpecifierParagraphSpacing style, it has no visual effect to the rendered text.The other attributes, such as line spacing and text alignment work perfectly.What could

When applying the kCTParagraphStyleSpecifierParagraphSpacing style, it has no visual effect to the rendered text. The other attributes, such as line spacing and text alignment work perfectly. What could I be doing 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 render the text using

CTFrameSetter frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attr);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake([[attr string] length], 0), the_drawing_cgrect, NULL);
CTFrameDraw(frame, context);


Are you sure your string has paragraph separators? kCTParagraphStyleSpecifierParagraphSpacing doesn't work on new lines. It requires actual \u2029.

Try this code to replace all newlines with paragraph separators:

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

精彩评论

暂无评论...
验证码 换一张
取 消