2010-02-17 1 views
6

NSTextView로 작업하고 있는데 하나의 요구 사항 중 하나는 탭 문자 '\ t'가 같아야한다는 것입니다. 너비는 4 칸입니다.내 탭 문자가 4 자의 너비로 그려 지도록 NSTextView의 내용을 다시 레이아웃하는 방법

AAAA 
    AAAA - 1 tab 
    AAAA - 4 spaces 

을 그리고 이것은 내가이 작업을 수행하는 방법입니다 :

그래서 텍스트 내용은 다음과 같을 것이다

// done when NSTextView first loaded and when 
// delegate's textDidBeginEditing gets called: (perhaps overkill, but is a work in progress). 
- (void)updateMyTextViewTextAttributes 
{ 
    NSMutableParagraphStyle* paragraphStyle = [[myTextView defaultParagraphStyle] mutableCopy]; 
    if (paragraphStyle == nil) { 
     paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    } 
    float charWidth = [[myFont screenFontWithRenderingMode:NSFontDefaultRenderingMode] advancementForGlyph:(NSGlyph) ' '].width; 
    [paragraphStyle setDefaultTabInterval:(charWidth * 4)]; 
    [paragraphStyle setTabStops:[NSArray array]]; 
    [myTextView setDefaultParagraphStyle:paragraphStyle]; 

    NSMutableDictionary* typingAttributes = [[myTextView typingAttributes] mutableCopy]; 
    [typingAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; 
    [typingAttributes setObject:scriptFont forKey:NSFontAttributeName]; 
    [myTextView setTypingAttributes:typingAttributes]; 
} 

이 적절한 배치를 할 수 있습니다뿐만 아니라 초기 텍스트로 표시 할 타이핑 속성을 동일하게 유지합니다.

문제는 최종 사용자가 글꼴을 변경할 수 있다는 것입니다. 그리고 이런 일이 발생하면 샘플 텍스트가 정렬되지 않게됩니다. 대부분 아래처럼 :

[smaller font] 
AAAA 
    AAAA - 1 tab 
    AAAA - 4 spaces 


[larger font] 
AAAA 
    AAAA - 1 tab 
    AAAA - 4 spaces 

나는 myTextView의 setNeedsDisplay를 호출 시도한 다음 avoidAdditionalLayout 매개 변수에 대해 NO로 avoidAdditionalLayout : YES 나는 그것이를 NSTextView의 setNeedsDisplayInRect를 호출 끝나는 것을 읽을 수있다. 이것은 아무것도 바뀌지 않았습니다.

myTextView에 새로운 myFont 세트가있을 때 my updateMyTextViewTextAttributes를 호출 해 보았습니다. 그건 아무것도 변하지 않습니다.

myTextView의 layoutManager에 myTextView의 textContainer에 대한 레이아웃을 보장하기 위해 시도해 보았습니다. 변경 없음.

이 시점에서 나는 다음에 무엇을 시도해야할지 모르겠다. 어떤 제안?

답변

3

Apple의 Douglas Davidson이 clues을 제공하여 [email protected] 이메일 목록을 통해 답변을 얻었습니다.

은 내가 updateMyTextViewTextAttributes 그래서 같은 기능을 업데이트하여 문제를 해결했습니다

- (void)updateMyTextViewTextAttributes 
{ 
   NSMutableParagraphStyle* paragraphStyle = [[myTextView defaultParagraphStyle] mutableCopy]; 

   if (paragraphStyle == nil) { 
       paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
   } 

   float charWidth = [[myFont screenFontWithRenderingMode:NSFontDefaultRenderingMode] advancementForGlyph:(NSGlyph) ' '].width; 
   [paragraphStyle setDefaultTabInterval:(charWidth * 4)]; 
   [paragraphStyle setTabStops:[NSArray array]]; 

   [myTextView setDefaultParagraphStyle:paragraphStyle]; 

   NSMutableDictionary* typingAttributes = [[myTextView typingAttributes] mutableCopy]; 
   [typingAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; 
   [typingAttributes setObject:scriptFont forKey:NSFontAttributeName]; 
   [myTextView setTypingAttributes:typingAttributes]; 

    /** ADDED CODE BELOW **/ 
   NSRange rangeOfChange = NSMakeRange(0, [[myTextView string] length]); 
   [myTextView shouldChangeTextInRange:rangeOfChange replacementString:nil]; 
   [[myTextView textStorage] setAttributes:typingAttributes range:rangeOfChange]; 
   [myTextView didChangeText]; 

    [paragraphStyle release]; 
    [typingAttributes release]; 
} 
1

advancementForGlyph:으로 공간 향상을 화면 글꼴 대신 글꼴에서 직접 계산해 보았습니까?

float charWidth = [myFont advancementForGlyph:(NSGlyph) ' '].width; 

화면 글꼴은 윈도우 서버 외부에서 직접 사용될 것은없고 :

화면 글꼴은 윈도우 서버 에 직접 사용하기위한 것입니다. 응용 프로그램 키트 개체에 을 사용하지 마십시오 (예 : , setFont : 메서드). 내부적으로 응용 프로그램 키트는 회전 또는 크기 조정되지 않는 한 글꼴의 글꼴에 대해 해당 화면 글꼴을 자동으로 사용합니다.

+0

나는 코드의 섹션을 발견 "[[에 myFont screenFontWithRenderingMode : NSFontDefaultRenderingMode] advancementForGlyph : (NSGlyph) '']. 폭;" 프로그래밍 방식으로 지정된 탭 너비를 처리하기 위해 설명 된 일부 코드는 온라인 상태입니다. "[myFont advancementForGlyph : (NSGlyph) ']] .width;"로 변경하면 행이 더 이상 일치하지 않습니다. 설명서에서 인용 한 내용이 있지만 원하는 기능이 없습니다 ... –