2012-05-24 98 views
0

我有这样的代码,在读取NSAttributedString的同时生成viewController数组。第一个循环后,即使有更多文本显示,函数CTFrameGetVisibleStringRange()也会返回0。CTFrameGetVisibleStringRange()返回0

- (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++; 
    } 
} 
+0

'CGPathAddRect(path,NULL,colRect)'应该是'CGPathAddRect(path,NULL,columnRect)'?确保你的'columnRect'足够大以容纳你最初设置的字体大小的单个字符,并且你应该'释放'循环中的'frame'对象,否则你有内存泄漏。 – neevek

+0

我很抱歉,为了清楚起见,我编辑了为col添加“-umn”的代码(colRect变成了columnRect)。实际上,变量名称是colRect。我发布了框架,但没有任何改变。我想知道为什么在这行代码中:“CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(textPos,0),path,NULL);”CFRangeMake函数在第二个循环时返回0 .. – Lolloz89

回答

0

您是否更改了对齐字符串的对齐方式?我有这个samme问题,发现它发生在某些情况下,当文本对齐设置为kCTJustifiedTextAlignment时,它应该适用于休息类型。

+0

我完全更改了代码..可能有一个直接的问题,理由在这里并不重要。不管怎样,谢谢你。 – Lolloz89

+0

我使用了这个相同的代码,因为你从ray教程中获得它,实际上理由不知何故很重要,但是我很高兴你以其他方式修复了它。 –