NSAttributedString kCTParagraphStyleSpecifierParagraphSpacing无效

时间:2011-04-14 20:02:42

标签: objective-c ios core-text

应用kCTParagraphStyleSpecifierParagraphSpacing样式时,它对渲染文本没有视觉效果。其他属性(如行间距和文本对齐)完美地工作。我能做错什么?

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);

我使用

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

2 个答案:

答案 0 :(得分:4)

你确定你的字符串有段落分隔符吗? kCTParagraphStyleSpecifierParagraphSpacing不适用于新行。它需要实际的\ u2029。

尝试使用此代码将所有换行替换为段落分隔符:

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

答案 1 :(得分:0)