ipad - 使用 Core Text 动态改变 View 高度

标签 ipad core-text

我有一个 View ,我想在其中使用 Text Core(在 iPad 上)绘制文本。当文本长大后,我想增加 View 的高度,但我不知道如何计算所需的框架高度。
我用它在 drawRect 方法中绘制文本:

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

我用它来创建一个属性字符串:
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 时,一开始一切都很好,但是当文本有更多行时,框架比文本越来越大,我希望它完全适合文本。我怎样才能做到?

最佳答案

要计算文本的高度,您是否尝试过使用 CTFramesetterSuggestFrameSizeWithConstraints ?

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

关于ipad - 使用 Core Text 动态改变 View 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7671598/

相关文章:

javascript - 为什么在 UIWebView 中呈现的 HTML 文件找不到我的 CSS 和 Javascript 文件?

ios - CTLineGetImageBounds 是一个非常广泛的操作

iphone - 如何将 DTCoreText 集成到新项目中?

core-text - 核心文本。如何计算属性字符串的边界框?

iOS13:有没有办法禁用新的滑动打字功能?

ios - 使用 CoreText 呈现字符串时的字距调整问题

iphone - 在 IOS 6 中设置 UITableView 的背景颜色

iphone - iPad 应用程序因不支持方向而被拒绝。如何解决问题?

objective-c - 独立方向 Nib

iphone - 授权 'application-identifier' 具有配置文件不允许的值