I found that compared with TextKit 1, NSTextLayoutManager does not provide any opportunity to customize line rectangles. In particular, I cannot do much about the frame of a NSTextLayoutFragment
. I am curious about the proper approach to:
-
limit the width and horizontal offset of a paragraph;
-
add spacing below and above a paragraph;
-
enclose a paragraph along an arbitrary path.
For the first question, I noticed that in the sample app, when layout comments, BubbleLayoutFragment
does not change the positions of any characters. While it did override renderingSurfaceBounds
, it merely expands the bounds, without moving its text contents. And there seems to be no way to do so.
For the second question, I noticed that paragraphSpacing
and paragraphSpacingBefore
set on the paragraph style seems to have no effect. According to the video, all layout fragments are stacked like a list. Therefore, this is an intended feature, right?
Right now, my solutions to the first two questions are:
- If I want to control the horizontal position of a paragraph, I could simply set
headIndent
andtrailIndent
on the paragraph style. - If I want to add spacing above or below a paragraph, I can add empty lines and set
minimalLineHeight
for the newline character.
Are those the recommended ways to achieve such effect?
Personally, I am not very interested in the third feature, enclosing a paragraph along an arbitrary path. However, I am indeed curious about how to add paragraph-level attachment, such as an block image/custom view between paragraphs. I think what I could do is:
-
add an empty new line and set its height to leave enough space for the custom view;
-
configure a
CALayer
or even a view and put it where the new line should be;
If I want to layout characters within a strange shape, I could compute the shape's height based on the width, using TextKit 1, and then insert the new line as in step 1.
Is this the intended way to approach such situation?