Dynamically change view height with Core Text

I have a view in which I want to draw a text with Text Core (on the iPad). When text grown up I'd like to increase a height of the view, but I don't know how to calculate needed height of frame. I use it to draw a text in drawRect method:

    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);

I tried to get a height of text using sizeWithFont and also that:

- (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));
}

I use that to create an attrubuted string:

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];

When I use sizeWithFont, at the begin everything is ok, but when text has more lines, the frame is bigger and bigger than a text and I want it to fit exactly the text. How can I make it?


To calculate the height of the text, have you tried using CTFramesetterSuggestFrameSizeWithConstraints ?

http://foobarpig.com/iphone/using-ctframesettersuggestframesizewithconstraints-and-sizewithfont-to-calculate-text-height.html

链接地址: http://www.djcxy.com/p/81902.html

上一篇: 我应该怎样设置JAVA

下一篇: 使用核心文本动态更改视图高度