2011-03-12 3 views
17

다음 스타일과 같이 텍스트를 그려야합니다. 이미지는 항상 오른쪽 상단에 있으며 텍스트는 이미지 주변에 있습니다.iOS에서 문자 배치로 이미지를 그릴 수 있습니까?

enter image description here

는 사람이 어떻게 iOS에서이를 구현하는 방법 말해 주시겠습니까? 핵심 텍스트를 사용해야합니까?

미리 감사드립니다.

답변

12

예, CoreText가이를 수행하는 가장 좋은 방법입니다. 이렇게하기위한 코드는 여기에 게시하는 데 약간 시간이 걸립니다. 올바른 방향으로 당신을 가리킬 수있는 코드는 기사에 있습니다 "Clipping a CGRect to a CGPath." 그것을하기위한 과정이 여전히 혼란 스러우면, ping me 그리고 마침내 나는 주제에 글을 쓰는 의미였습니다.

+0

나는 당신의 도움이 필요하다고 생각합니다. 나는 당신의 블로그 포스트에있는 코드를 이해하지 못한다. 이 주제에 대한 기사를 써주시겠습니까? 고맙습니다! – nonamelive

+0

이것에 대해서는 잊지 않았습니까? 그것을 함께 모으기 위해 약간의 시간이 걸렸습니다. –

+7

조금 거칠지 만 잘하면 올바른 방향으로 움직일 수 있기를 바랍니다. http://robnapier.net/blog/wrapping-text-around-shape-with-coretext-540 –

1

1 단계 :

UIView의

로부터 상속 된 객체를 만들기 2 단계 : 무시 - (무효)의 drawRect : (CGRect) RECT 방법

3 단계 :이 방법

/* Define some defaults */ 
float padding = 10.0f; 

/* Get the graphics context for drawing */ 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

/* Core Text Coordinate System is OSX style */ 
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); 
CGContextTranslateCTM(ctx, 0, self.bounds.size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGRect textRect = CGRectMake(padding, padding, self.frame.size.width - padding*2, self.frame.size.height - padding*2); 

/* Create a path to draw in and add our text path */ 
CGMutablePathRef pathToRenderIn = CGPathCreateMutable(); 
CGPathAddRect(pathToRenderIn, NULL, textRect); 

/* Add a image path to clip around, region where you want to place image */ 
CGRect clipRect = CGRectMake(padding, self.frame.size.height-50, 50, 40); 
CGPathAddRect(pathToRenderIn, NULL, clipRect); 

/* Build up an attributed string with the correct font */ 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.Text]; 

//setFont 
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:10].fontName, [UIFont systemFontOfSize:10].lineHeight, NULL); 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName,font); 

//set text color 
CGColorRef _white=[UIColor whiteColor].CGColor; 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)(attrString), CFRangeMake(0, attrString.length),kCTForegroundColorAttributeName, _white); 

/* Get a framesetter to draw the actual text */ 
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) attrString); 
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL); 

/* Draw the text */ 
CTFrameDraw(frame, ctx); 

/* Release the stuff we used */ 
CFRelease(frame); 
CFRelease(pathToRenderIn); 
CFRelease(fs); 
에 다음 코드를 추가합니다

4 단계 : 다음과 같이 사용하십시오.

TextLayoutView *TextWrappingImage=[[TextLayoutView alloc] init...your own constructor...]; 

TextWrappingImage.backgroundColor=[UIColor clearColor]; 

[cell addSubview:TextWrappingImage]; //Add as subview where you want 
0

비슷한 문제. 표준 마크 업 태그를 사용하여 텍스트로 그래픽 텍스트 문서를 HTML로 작성하고 HTML 및 PNG를 프로젝트에 추가 한 다음 UIWebView를 사용하여이를 표시하여 해결했습니다. :-)

+0

+1. 가장 우아한 해결책은 아니지만이 방법을 사용하면 div 요소에서이 간단한 CSS를 처리하는 코드 한 줄을 사용할 수 있습니다. 'width : 95px; 높이 : 95px; 여백 : 5px 5px 5px 0px; 테두리 : 1 픽셀 단색 #eee; 왼쪽으로 뜨다; 배경 : URL (% @) 중심점; background-repeat : no-repeat; background-size : contains;'제 의견으로는 훨씬 작고 쉽게 구현할 수 있습니다. 감사 – thefoyer

관련 문제