gpt4 book ai didi

iphone - CTFrameGetLineOrigins 中的错误?

转载 作者:行者123 更新时间:2023-11-28 20:37:40 24 4
gpt4 key购买 nike

我正在使用 Apple 的 SimpleTextInput 示例中的以下代码来确定光标的框架。我正在使用 CTFrameGetLineOrigins,我注意到它返回的 y 值不正确(无论在哪一行,它都显示 .336)。我的应用程序与 SimpleTextInput 略有不同,因为我将文本垂直居中,因此文本框架不会直接从原点开始。但是,为什么它给我每一行的值(value)?这是错误吗?

for (int i = 0; i < [lines count]; i++) 
{
CTLineRef line = (CTLineRef) [lines objectAtIndex:i];
CFRange range = CTLineGetStringRange(line);
NSInteger localIndex = index - range.location;

if (localIndex >= 0 && localIndex <= range.length)
{
CGPoint origin;
CGFloat ascent, descent;
CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
CTFrameGetLineOrigins(self.textFrame, CFRangeMake(i, 0), &origin);
CGFloat xPos = origin.x + CTLineGetOffsetForStringIndex(line, index, NULL);
return CGRectMake(xPos, origin.y - descent, 3, ascent + descent);
}
}

- (void) drawRect:(CGRect)rect
{
CTFramesetterRef textFramesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.text);
CGMutablePathRef textFrameMutablePath = CGPathCreateMutable();
CGPathAddRect(textFrameMutablePath, NULL, self.bounds);
self.textFrame = CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textFrameMutablePath, NULL);

CGFloat textFrameHeight = [self calculateHeightForTextFrame];
CGRect textFrameRect = CGRectMake(0, (self.bounds.size.height / 2) - (textFrameHeight / 2), self.bounds.size.width, textFrameHeight);

CGPathRelease(textFrameMutablePath);
textFrameMutablePath = NULL;

CFRelease(self.textFrame);
self.textFrame = NULL;

textFrameMutablePath = CGPathCreateMutable();
CGPathAddRect(textFrameMutablePath, NULL, textFrameRect);
self.textFrame = CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textFrameMutablePath, NULL);

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

CTFrameDraw(self.textFrame, context);

CGPathRelease(textFrameMutablePath);
CFRelease(textFramesetter);

[self cursorPositionChanged];
}

最佳答案

Take out CTFrameGetLineOrigins() out of loop to get all line origins like :

NSArray *lines = (__bridge id)CTFrameGetLines(frame);
CGPoint origins[lines.count];
CTFrameGetLineOrigins(frame, CFRangeMake(0, lines.count), origins); //this will fill all line origins in origins[] buffer.

for (int i = 0; i < [lines count]; i++)
{
CTLineRef line = (__bridge CTLineRef) [lines objectAtIndex:i];
CFRange range = CTLineGetStringRange(line);

// CGPoint origin; // moved out of loop
CGFloat ascent, descent;
CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
// CTFrameGetLineOrigins(frame, CFRangeMake(i, 1), &origin); // moved to out of loop
CGFloat xPos = origins[i].x; //  + CTLineGetOffsetForStringIndex(line, index, NULL);
NSLog(@"%@", NSStringFromCGRect(CGRectMake(xPos + origins[i].x, origins[i].y - descent, 3, ascent + descent)));
}

关于iphone - CTFrameGetLineOrigins 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9809607/

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