2011-10-06 44 views
3

我有一个视图,我想用Text Core(在iPad上)绘制文本。当文字长大了,我想增加视图的高度,但我不知道如何计算框架所需的高度。 我用它来绘制的drawRect方法的文字:使用Core Text动态更改视图高度

CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextTranslateCTM(context, 0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)currentTexts); 
    CGMutablePathRef textPath = CGPathCreateMutable(); 
    CGRect textRect = CGRectMake(PADDING, PADDING, self.frame.size.width - 2 * PADDING, self.frame.size.height - 2 * PADDING); 
    CGPathAddRect(textPath, NULL, textRect); 
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), textPath, NULL); 

    CTFrameDraw(textFrame, context); 

    CFRelease(textFrame); 
    CGPathRelease(textPath); 
    CFRelease(framesetter); 

我试着用sizeWithFont获取文本的高度,也即:

- (CGSize) measureFrame: (CTFrameRef) frame 
{ 
CGPathRef framePath = CTFrameGetPath(frame); 
CGRect frameRect = CGPathGetBoundingBox(framePath); 

CFArrayRef lines = CTFrameGetLines(frame); 
CFIndex numLines = CFArrayGetCount(lines); 

CGFloat maxWidth = 0; 
CGFloat textHeight = 0; 

// Now run through each line determining the maximum width of all the lines. 
// We special case the last line of text. While we've got it's descent handy, 
// we'll use it to calculate the typographic height of the text as well. 
CFIndex lastLineIndex = numLines - 1; 
for(CFIndex index = 0; index < numLines; index++) 
{ 
    CGFloat ascent, descent, leading, width; 
    CTLineRef line = (CTLineRef) CFArrayGetValueAtIndex(lines, index); 
    width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading); 

    if(width > maxWidth) 
    { 
     maxWidth = width; 
    } 

    if(index == lastLineIndex) 
    { 
     // Get the origin of the last line. We add the descent to this 
     // (below) to get the bottom edge of the last line of text. 
     CGPoint lastLineOrigin; 
     CTFrameGetLineOrigins(frame, CFRangeMake(lastLineIndex, 1), &lastLineOrigin); 

     // The height needed to draw the text is from the bottom of the last line 
     // to the top of the frame. 
     textHeight = CGRectGetMaxY(frameRect) - lastLineOrigin.y + descent; 
    } 
} 

// For some text the exact typographic bounds is a fraction of a point too 
// small to fit the text when it is put into a context. We go ahead and round 
// the returned drawing area up to the nearest point. This takes care of the 
// discrepencies. 
return CGSizeMake(ceil(maxWidth), ceil(textHeight)); 
} 

我用它来创建一个attrubuted字符串:

CTParagraphStyleSetting setting[1] = { 
    {kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(CGFloat), &minimumLineSpacing} 
}; 

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 1); 

NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys: 
         (id)textColor.CGColor, kCTForegroundColorAttributeName, 
         (id)currentFont, kCTFontAttributeName, 
         (id)paragraphStyle, kCTParagraphStyleAttributeName, 
         nil]; 

当我使用sizeWithFont时,在开始时一切正常,但是当文本有更多的行时,框架比文本更大更大,我希望它完全适合文本。我怎么做到的?

回答