2009-03-03 5 views
10

NSBezierPath 클래스를 사용하여 내 사용자 정의보기 클래스의 drawRect 함수에서 모양을 그리는 방법을 알아 냈습니다. 그러나 텍스트 그리는 방법을 알 수없는 것 같습니다.Objective-C를 사용하여 사용자 지정보기에서 텍스트를 그리는 방법은 무엇입니까?

NSText *text = [NSText new]; 
[text setTextColor: [NSColor yellowColor]]; 
[text setText: @"Hello!"]; 

내가 어디립니다 NSText 오브젝트를 이야기하는 NSRect 또는 NSPoint를 제공해야 할 수도 있습니다 같은데요 : 다음 코드는 지금까지합니다 (의 drawRect 함수에 위치) 텍스트를 그리기위한이 무엇 자체,하지만 어떻게 해야할지 코코아 문서에서 아무것도 찾을 수 없습니다.

답변

22

당신은이 라인을 따라 뭔가를 시도 할 수 :

//note we are using the convenience method, so we don't need to autorelease the object 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:26], NSFontAttributeName,[NSColor blackColor], NSForegroundColorAttributeName, nil]; 

NSAttributedString * currentText=[[NSAttributedString alloc] initWithString:@"Cat" attributes: attributes]; 

NSSize attrSize = [currentText size]; 
[currentText drawAtPoint:NSMakePoint(yourX, yourY)]; 
+0

완벽! 도와 줘서 고마워. –

2

NSText은 도면이다 (특히,를 NSTextView의 슈퍼 클래스).

속성 (글꼴, 색상, 단락 스타일 등)이 있거나없는 텍스트를 그리는 데는 여러 가지 방법이 있습니다. AppKit's additions to NSStringto NSAttributedString을 참조하십시오.

관련 문제