Line spacing and paragraph alignment in CoreText

六月ゝ 毕业季﹏ 提交于 2020-01-21 03:35:08

问题


I am using CoreText to render multiple columns of text. However, when I set the first letter of the 1st paragraph to a bold, larger font than the rest of the text, I incur 2 issues (both visible in the attached image):

  1. The spacing underneath the first line is too big (I understand that this is because the 1st character could be a g,y,p,q etc.

  2. Lines below the first line now do not line up with corresponding lines in the next column.

Any advice on how to overcome these 2 issues would be greatly appreciated, thank you.


回答1:


According to the documentation kCTParagraphStyleSpecifierMaximumLineHeight should have solved the problem, but unfortunately does not seem to work at least on IOS 4.3.

CTParagraphStyleSetting theSettings[5] = 
{
    { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &spaceBetweenParaghraphs },
    { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &topSpacing },
    { kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &spaceBetweenLines },
    { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &lineHeight},
    { kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &lineHeight}
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, 5);

To be fair documentation says it's available in OS v10.5 and later.

kCTParagraphStyleSpecifierMaximumLineHeight:
The maximum height that any line in the frame will occupy, regardless of the font size or size of any attached graphic. Glyphs and graphics exceeding this height will overlap neighboring lines. A maximum height of 0 implies no line height limit. This value is always nonnegative.
Type: CGFloat.
Default: 0.0.
Application: CTFramesetter.
Available in Mac OS X v10.5 and later.
Declared in CTParagraphStyle.h.




回答2:


It seems the only way to fix this is with a workaround, which is to create 3 frames for the first column,1 for the W, 1 for the rest of the first sentence and 1 for the rest of the first column.



来源:https://stackoverflow.com/questions/5312962/line-spacing-and-paragraph-alignment-in-coretext

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