我正在使用Apple的SimpleTextInput
示例中的以下代码来确定光标的帧。我正在使用CTFrameGetLineOrigins
并且我注意到它返回的y值是不正确的(无论哪条线都在,它显示.336)。我的应用程序与SimpleTextInput
略有不同,因为我将文本垂直居中,因此文本框架不会直接从原点开始。但是,为什么它给我这个每行的价值呢?这是一个错误吗?
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];
}
答案 0 :(得分:2)
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)));
}
答案 1 :(得分:1)
你的范围是0元素长:
CFRangeMake(i, 0)
你想让它长1个元素:
CFRangeMake(i, 1)
编辑:
我刚刚尝试了以下代码,并且没有问题为每一行获取不同的来源。我拿出了你的“索引”代码,因为我不知道应该设置什么索引,我想证明我可以得到所有的行起源(我可以)。
CTFramesetterRef
framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFArrayRef paths = [self copyPaths];
CFIndex pathCount = CFArrayGetCount(paths);
CFIndex charIndex = 0;
for (CFIndex pathIndex = 0; pathIndex < pathCount; ++pathIndex) {
CGPathRef path = CFArrayGetValueAtIndex(paths, pathIndex);
CTFrameRef
frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(charIndex, 0),
path,
NULL);
NSArray *lines = (__bridge id)CTFrameGetLines(frame);
for (int i = 0; i < [lines count]; i++)
{
CTLineRef line = (__bridge CTLineRef) [lines objectAtIndex:i];
CFRange range = CTLineGetStringRange(line);
CGPoint origin;
CGFloat ascent, descent;
CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
CTFrameGetLineOrigins(frame, CFRangeMake(i, 1), &origin);
CGFloat xPos = origin.x; // + CTLineGetOffsetForStringIndex(line, index, NULL);
NSLog(@"%@", NSStringFromCGRect(CGRectMake(xPos, origin.y - descent, 3, ascent + descent)));
}
在我的情况下输出:
2012-03-21 18:28:19.834 Columns[9370:f803] {{0, 929.24}, {3, 12}}
2012-03-21 18:28:19.835 Columns[9370:f803] {{0, 914.24}, {3, 12}}
2012-03-21 18:28:19.835 Columns[9370:f803] {{0, 899.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 884.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 869.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 854.24}, {3, 12}}
2012-03-21 18:28:19.837 Columns[9370:f803] {{0, 839.24}, {3, 12}}
2012-03-21 18:28:19.837 Columns[9370:f803] {{0, 824.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 809.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 794.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 779.24}, {3, 12}}
2012-03-21 18:28:19.839 Columns[9370:f803] {{0, 764.24}, {3, 12}}
...
答案 2 :(得分:0)
奇怪的是,我似乎通过替换:
解决了这个问题CGPathAddRect(textFrameMutablePath, NULL, self.bounds);
与
CGPathAddRect(textFrameMutablePath, NULL, CGRectInset(self.bounds, self.horizontalInset, self.verticalInset));
不知道为什么会有效。