在不使用换行符的情况下定义核心文本段落? [英] Define Core Text paragraphs without using newline character?

查看:57
本文介绍了在不使用换行符的情况下定义核心文本段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为NSAttributedString中的段落应用适当的顶部和底部间距时,我遇到了一个小问题.我正在使用此代码来设置段落属性:

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. 除非属性范围的开头有换行符\ n,否则各段之间不能相互放置,而是以内联"形式流动.
  2. 当我添加换行符时,段落将正确地放置在另一个段落的下面,但是换行符的行高将被添加到间距"ParagraphSpacing"的间隙中.
  3. kCTParagraphStyleSpecifierParagraphSpacingBefore也影响段落范围内的换行符.

什么Core Text布局引擎将其解释为段落指示符?是属性字符串中的每个换行符吗?

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

推荐答案

在Core Text中,换行符( \ n )结束了一个段落.我认为没有办法告诉它使用其他字符.

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.

如果您连续放置两个换行符,则Core Text的作用就好像您有一个空的段落一样,因此它将以多余的间距放置.

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.

确保每个段落后只有一个换行符.如果您的段落之间仍然留有太多空间,则需要为 kCTParagraphStyleSpecifierParagraphSpacingBefore kCTParagraphStyleSpecifierParagraphSpacing (或两者)使用较小的值.

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

这篇关于在不使用换行符的情况下定义核心文本段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆