CTRunGetImageBounds 返回不准确的结果
我正在使用 Core Text 来绘制一些文本。我想获取各种运行边界,但是当我调用 CTRunGetImageBounds
时,返回的矩形大小正确,但位置错误。具体来说,行原点位于整个文本的末尾。
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
self.transform = CGAffineTransformMakeScale(1.0, -1.0);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
[[UIColor whiteColor] set];
CGContextFillRect(context, self.bounds);
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"Blue should be underlined."];
NSRange blueRange = NSMakeRange(0, 4);
[attrString beginEditing];
//make all text size 20.0
[attrString addAttribute:(NSString *)kCTFontAttributeName value:(id)CTFontCreateWithName((CFStringRef)@"Helvetica", 20.0, NULL) range:NSMakeRange(0, [attrString length])];
//make the text appear in blue
[attrString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:blueRange];
//next make the text appear with an underline
[attrString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:blueRange];
[attrString endEditing];
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);
[[UIColor redColor] set];
CGContextFillRect(context, bounds);
CGPathAddRect(path, NULL, bounds);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CTFrameDraw(frame, context);
for (id lineObj in (NSArray *)CTFrameGetLines(frame)) {
CTLineRef line = (CTLineRef)lineObj;
for (id runObj in (NSArray *)CTLineGetGlyphRuns(line)) {
CTRunRef run = (CTRunRef)runObj;
CGRect runBounds = CTRunGetImageBounds(run, context, CFRangeMake(0, 0));
NSLog(@"bounds: %@", NSStringFromCGRect(runBounds));
[[UIColor greenColor] set];
CGContextFillRect(context, runBounds);
}
}
CFRelease(framesetter);
CFRelease(frame);
[attrString release];
}
产生:
I am using Core Text to draw some text. I would like to get the various run bounds, but when I call CTRunGetImageBounds
, the rect that is returned is the correct size, but in the wrong location. Specifically, the line origin is at the end of the text as a whole.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
self.transform = CGAffineTransformMakeScale(1.0, -1.0);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
[[UIColor whiteColor] set];
CGContextFillRect(context, self.bounds);
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"Blue should be underlined."];
NSRange blueRange = NSMakeRange(0, 4);
[attrString beginEditing];
//make all text size 20.0
[attrString addAttribute:(NSString *)kCTFontAttributeName value:(id)CTFontCreateWithName((CFStringRef)@"Helvetica", 20.0, NULL) range:NSMakeRange(0, [attrString length])];
//make the text appear in blue
[attrString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:blueRange];
//next make the text appear with an underline
[attrString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:blueRange];
[attrString endEditing];
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);
[[UIColor redColor] set];
CGContextFillRect(context, bounds);
CGPathAddRect(path, NULL, bounds);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CTFrameDraw(frame, context);
for (id lineObj in (NSArray *)CTFrameGetLines(frame)) {
CTLineRef line = (CTLineRef)lineObj;
for (id runObj in (NSArray *)CTLineGetGlyphRuns(line)) {
CTRunRef run = (CTRunRef)runObj;
CGRect runBounds = CTRunGetImageBounds(run, context, CFRangeMake(0, 0));
NSLog(@"bounds: %@", NSStringFromCGRect(runBounds));
[[UIColor greenColor] set];
CGContextFillRect(context, runBounds);
}
}
CFRelease(framesetter);
CFRelease(frame);
[attrString release];
}
Produces:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我想出的。这是完全准确的。
Here is what I came up with. This is completely accurate.
在使用 CTRunGetImageBounds() 或 CTLineDraw() 之前,您需要在绘制或计算每条线之前通过调用 CGContextSetTextPosition() 设置起始文本位置。原因是 CTLine 不知道自己从哪里开始绘制(或计算),它使用最后一个文本位置,因此您需要给它一个 XY 起点。要获取帧的线数组的原点,请使用点数组调用 CTFrameGetLineOrigins()。然后,在绘制或计算每条线之前 - 通过 CGContextSetTextPosition() 设置该线的原点。
Before you can use CTRunGetImageBounds() or CTLineDraw() you need to set the starting text position with a call to CGContextSetTextPosition() before drawing or computing each line. The reason is a CTLine has no idea where to start drawing (or computing) on its own, it uses the last text position, so you need to give it an XY starting point. To get the origins for a frame's array of lines, call CTFrameGetLineOrigins() with an array of points. Then, before drawing or computing each line - set the origin of that line via CGContextSetTextPosition().