簡體   English   中英

CTFrameGetVisibleStringRange()返回0

[英]CTFrameGetVisibleStringRange() returns 0

我有這段代碼在讀取NSAttributedString時會生成一個viewController數組。 在第一個周期之后,即使還有更多文本要顯示,函數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++;
     }
}

您是否更改了attributedString的對齊方式? 我遇到了這個samme問題,發現當文本對齊方式設置為kCTJustifiedTextAlignment時,在某些情況下會發生這種情況,它在其余類型上應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM