bottom instead of left

I'm working on a view that's implementing a multi-column text layout using CoreText (using CTFramesetter ).

CoreText usually fills each frame completely, so when I call CTFramesetterCreateFrame with three rects that make up my columns, I get a layout that's similar to the following image:

So the left column is filled completely, the middle column partially and the right column is empty. But instead, I'd like the text to distribute over the three columns so that they take up the least vertical space possible, like in this image:

How to achieve this with CoreText?

I don't mind going low-level here, even drawing each CTRun by hand is an option if necessary.

One idea I came up with would be to create a large frame with the width of a column and then figure out which CTLine to draw in which column. But this has a few limitations:

  • It would only work if all columns had the same width.
  • It does not work with clipping paths.
  • Unfortunately, I'll need to use clipping paths (as in kCTFrameClippingPathsAttributeName ) so this idea is out. I could live the fixed column width limitation, though.

    Another idea would be to reduce the height until the last frame overflows but that's a pretty brute-force way that surely wastes resources.

    (BTW, due to compability requirements the use of TextKit classes like NSTextStorage isn't possible; the resulting view is intended to be used on Mac and iOS, but it needs to work on iOS < 7)


    Since there doesn't seem to be a non-expensive way to solve this, here's how I've done it:

    I did go with the "reduce the height until the last frame overflows" approach. To reduce the height, I simply have another clipping path ( kCTFrameClippingPathsAttributeName ) which is a rectangle that fills the bottom of the view to the required height.

    The probably most expensive but simple way would have been to increase the rectangle height until finally the text doesn't fit inside the last frame any more.

    Instead I've implemented a binary search for that. For my demo app, I usually find the correct height after 8-10 recursions which still is expensive but at least it's pixel-perfect and doesn't rely on any other information other than "did the last frame overflow".

    链接地址: http://www.djcxy.com/p/14382.html

    上一篇: ios viewcontroller视图图片生活在Mutable Array中

    下一篇: 底部而不是左边