2012-05-24 123 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教程中獲得它,實際上理由不知何故很重要,但是我很高興你以其他方式修復了它。 –