Define Core Text paragraphs without using newline character?

余生颓废 提交于 2020-01-07 06:19:21

问题


I am having a trivial problem with applying proper top and bottom spacing for the paragraphs in NSAttributedString. I am usng this code to set paragraph attributes:

int sf = sizeof(CGFloat);
        CTParagraphStyleSetting settings[ParagraphStylesSupported] = 
            {
                { kCTParagraphStyleSpecifierAlignment, sizeof(QuartzTextAlignment), &style.textAlignment },
                { kCTParagraphStyleSpecifierParagraphSpacingBefore, sf, &marginTop},
                { kCTParagraphStyleSpecifierParagraphSpacing, sf, &marginBot},
                { kCTParagraphStyleSpecifierMinimumLineHeight, sf, &lineHeight},

                { kCTParagraphStyleSpecifierLineSpacing, sf, &lineSpacing},
                { kCTParagraphStyleSpecifierFirstLineHeadIndent, sf, &style.firstLineIndent},

            };

            CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, ParagraphStylesSupported);


            [string addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:item.range];
            CFRelease(paragraphStyle);

Text properties are being applied as expected. But there are few problems with paragraph alignment:

  1. Paragraphs are not being places after each other but flow 'inline' unless there is a newline \n character at the beginning of the attribute range.
  2. When I add the newline character, then paragraphs are being placed correctly one below another, but newline line height is being added to the spacing 'ParagraphSpacing' gaps.
  3. kCTParagraphStyleSpecifierParagraphSpacingBefore affects also the newlines character inside the paragraph range.

What Core Text layout engine interprets as a paragraph indicator? Is it every newline character in the attributed string?


回答1:


In Core Text, the newline character (\n) ends a paragraph. I don't think there is a way to tell it to use a different character.

If you put two newlines in a row, then Core Text will act as if you have an empty paragraph, so it will put in extra spacing.

Make sure you only have one newline after each paragraph. If your paragraphs still have too much space between them, you need to use a smaller value for kCTParagraphStyleSpecifierParagraphSpacingBefore or kCTParagraphStyleSpecifierParagraphSpacing (or both).



来源:https://stackoverflow.com/questions/9343787/define-core-text-paragraphs-without-using-newline-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!