일부 텍스트를 그리기 위해 핵심 텍스트를 사용하고 있습니다. 다양한 실행 범위를 얻고 싶습니다만, CTRunGetImageBounds
을 호출하면 반환되는 rect가 올바른 크기이지만 잘못된 위치에 있습니다. 특히, 라인 원점은 텍스트 전체에 있습니다.부정확 한 결과를 반환하는 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];
}
는 생산 :
여기
전자가 CGContext를 필요로하지 않기 때문에 대신 이미지 경계의이 답변에 표시된 것처럼 인쇄상의 경계를 사용하는 것이 좋습니다. 또한 getImageBounds 함수는 더 느린 순서입니다. – Cocoanetics
나중에 2 시간 씩 인터넷 검색, 나는 성배를 우연히 발견합니다. 감사. –
위의 코드는 부분적으로 작동합니다. 오류 결과를 반환하는 코드는 다음과 같습니다. runBounds.origin.y = origins [lineIndex] .y + rect.origin.y; runBounds.origin.y - = descent; 앞의 모든 코드가 width, height, origin.x ... origin.y의 올바른 결과를 반환하더라도 누구나 그 이유를 알고 있습니까? 어떤 도움을 주셔서 감사합니다. – stefanosn