2011-04-10 3 views
10

다중 라인 NSTextField를 만드는 방법은 무엇입니까? 업데이트 : "Wrapped Text Field"라는 IB 특수 유형의 NSTextField에서 발견했습니다. 여러 줄이지만 줄 바꿈이 필요하면 Ctrl + Enter를 눌러야합니다. 하지만 줄 바꿈을하려면 Enter 만 누르기를 원합니다. 내가 어떻게 해?코코아 : 여러 줄 NSTextField 만드는 방법?

+4

당신은 단지를 NSTextView을 사용할 수 없습니다 : 여기

은 기술 노트에서 예를 위임 메시지인가? 여러 줄 텍스트 필드입니다. –

+0

자동 구속 조건이있는 다른 솔루션의 경우 내 대답을 볼 수 있습니다. [https://stackoverflow.com/a/48705158/7677598](https://stackoverflow.com/a/48705158/7677598) – abdullahselek

답변

11

인터페이스 빌더에서 유일하게이 동작을 지정하는 방법은 없습니다를 표시 할 수 있습니다대로 그 내용을 채우기 위해 그것을 대신 NSStringNSAttributedString 객체를 제공해야합니다. 이 기술 노트 QA1454에 설명 된대로 대리인 메시지로 처리 할 수 ​​있습니다.

- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector 
{ 
    BOOL result = NO; 

    if (commandSelector == @selector(insertNewline:)) 
    { 
     // new line action: 
     // always insert a line-break character and don’t cause the receiver to end editing 
     [textView insertNewlineIgnoringFieldEditor:self]; 
     result = YES; 
    } 
    else if (commandSelector == @selector(insertTab:)) 
    { 
     // tab action: 
     // always insert a tab character and don’t cause the receiver to end editing 
     [textView insertTabIgnoringFieldEditor:self]; 
     result = YES; 
    } 

    return result; 
} 
6

NSTextView을 사용하여, 그 복수 행 NSTextField 일종의, 그것은 서브 클래스 NSText 내가 틀렸다면 정정하십시오. NSTextView에는 NSTextStorage이 있으며 이는 NSAttributedString의 하위 클래스입니다. 는 등 색상

[[yourTextView textStorage] setAttributedString:attrStr]; 
관련 문제