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에 대한 레이아웃을 보장하기 위해 시도해 보았습니다. 변경 없음.
이 시점에서 나는 다음에 무엇을 시도해야할지 모르겠다. 어떤 제안?
나는 코드의 섹션을 발견 "[[에 myFont screenFontWithRenderingMode : NSFontDefaultRenderingMode] advancementForGlyph : (NSGlyph) '']. 폭;" 프로그래밍 방식으로 지정된 탭 너비를 처리하기 위해 설명 된 일부 코드는 온라인 상태입니다. "[myFont advancementForGlyph : (NSGlyph) ']] .width;"로 변경하면 행이 더 이상 일치하지 않습니다. 설명서에서 인용 한 내용이 있지만 원하는 기능이 없습니다 ... –