0

I have this code that generates an array of viewController while reading an NSAttributedString. After the first cycle, the function CTFrameGetVisibleStringRange() returns 0 even if there is more text to display.

- (void)buildFrames
{
/*here we do some setup - define the x & y offsets and create an empty frames array */
    float frameXOffset = 20; 
    float frameYOffset = 20;

    self.frames = [NSMutableArray array];

    //buildFrames continues by creating a path and a frame for the view's bounds (offset slightly so we have a margin).
    CGMutablePathRef path = CGPathCreateMutable(); 
    // create an insect rect for drawing
    CGRect textFrame = CGRectInset(self.bounds, frameXOffset, frameYOffset); 
    CGPathAddRect(path, NULL, textFrame );// add it to the path

    // Create a frame setter with my attributed String
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);

    //This section declares textPos, which will hold the current position in the text. 
    //It also declares columnIndex, which will count how many columns are already created.
    int textPos = 0; 
    int columnIndex = 0;

    while (textPos < [attributedString length]) { 
        //The while loop here runs until we've reached the end of the text. Inside the loop we create a column bounds: colRect is a CGRect which depending on columnIndex holds the origin and size of the current column. Note that we are building columns continuously to the right (not across and then down).

        CGPoint colOffset = CGPointMake(frameXOffset , frameYOffset);
        CGRect columnRect = CGRectMake(0, 0 , textFrame.size.width, textFrame.size.height);

        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, colRect);

        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), path, NULL);
        CFRange frameRange = CTFrameGetVisibleStringRange(frame); 

       // MY CUSTOM UIVIEW
        LSCTView* content = [[[LSCTView alloc] initWithFrame: CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];
        content.backgroundColor = [UIColor clearColor];
        content.frame = CGRectMake(colOffset.x, colOffset.y, columnRect.size.width, columnRect.size.height) ;

    /************* CREATE A NEW VIEW CONTROLLER WITH view=content *********************/

        textPos += frameRange.length;

        CFRelease(path);

        columnIndex++;
     }
}
Lolloz89
  • 2,809
  • 2
  • 26
  • 41
  • `CGPathAddRect(path, NULL, colRect)` should be `CGPathAddRect(path, NULL, columnRect)`? Make sure your `columnRect` is big enough to hold a single character of the font size you initially set, and you should `release` the `frame` object in the loop, otherwise you have memory leaks. – neevek May 27 '12 at 01:38
  • I'm sorry, I edited the code adding "-umn" to col (colRect became columnRect) for clarity. Actually, the variable name is colRect. I released the frame but nothing changed. I wonder why in this line of code:" CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), path, NULL); " The function CFRangeMake returns 0 at the second while cycle.. – Lolloz89 May 27 '12 at 10:39

1 Answers1

0

Did you change alignment for attributedString? I had this samme issue and found that it occurs in some cases when text alignment is set to kCTJustifiedTextAlignment, it should works fine with rest types.

Michal Gumny
  • 1,770
  • 1
  • 16
  • 24
  • I completely changed the code.. Probably there were a rect-making issue, the justification doesn't matter here. Thank you anyway. – Lolloz89 Jun 01 '12 at 07:40
  • I used this same code cause you have it from ray tutorial and actually justification somehow did matter, but I'm glad that you fixed it else way. – Michal Gumny Jun 01 '12 at 15:25