gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:37:33 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com