2013-03-13 3 views
1

내가 잘못 여기 일을해야합니다 textview with "double vision"를 NSTextView 복시

내 코코아 응용 프로그램은 차례로 텍스트 뷰가있는 사용자 정의보기 주위에있는 ScrollView 있습니다. 나는 단지 하나의 "This is a string"을 볼 것으로 예상하지만 구석에 여분의 문자열이 있습니다. 코드를 매우 작게 만들었지 만 여전히 내 오류가 무엇인지 이해하지 못하기 때문에 여기에 제가 단서를 찾고 있습니다.

사용자 정의보기의보기 컨트롤러는 다음과 같습니다. 여기서는 간단하게하기 위해 project에 대한 링크를 보여줍니다.

[super drawRect:rect]; 

을 당신은 당신의 draw 함수의 텍스트를 직접 그리는 :

#import "TTTSimpleCtrlView.h" 

@interface TTTSimpleCtrlView() 

@property (strong,nonatomic) NSTextView *tv1; 
@property (strong,nonatomic) NSTextStorage *ts; 

@end 

@implementation TTTSimpleCtrlView 

- (void) awakeFromNib { 
    NSFont *font = [NSFont fontWithName:@"Courier New Bold" size:20.0f]; 
    NSMutableParagraphStyle *styleModel = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [styleModel setLineHeightMultiple:1.0]; 
    // [styleModel setLineSpacing:fontRect.size.height * 2]; 
    NSDictionary *textAttrs = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, 
            [NSColor blackColor] ,NSForegroundColorAttributeName, 
            [NSColor whiteColor], NSBackgroundColorAttributeName, 
            styleModel, NSParagraphStyleAttributeName, 
            nil]; 
    NSString *pilcrowStr = @"This is a test."; 
    NSAttributedString *s = [[NSAttributedString alloc] initWithString:pilcrowStr attributes:textAttrs]; 
    NSRect rect = [s boundingRectWithSize:NSMakeSize(INFINITY,INFINITY)options:0]; 
    NSLayoutManager *lm = [[NSLayoutManager alloc] init]; 
    NSTextContainer *tc = [NSTextContainer new]; 

    [tc setContainerSize:s.size]; 
    [lm addTextContainer:tc]; 

    _ts = [[NSTextStorage alloc] init]; 
    [_ts setAttributedString:s]; 

    [_ts addLayoutManager:lm]; 
    [lm replaceTextStorage:_ts]; 

    rect.origin.x = 10; 
    rect.origin.y = rect.size.height; 
    NSTextView *v = [[NSTextView alloc] initWithFrame:rect textContainer:tc]; 
    [v setDrawsBackground:YES]; 
    [self addSubview:v]; 
} 

- (BOOL) isFlipped { 
    return YES; 
} 

- (void)drawRect:(NSRect)rect 
{ 
    NSLog(@"drawRect & %lu subviews",self.subviews.count); 
    for (NSTextView *v in self.subviews) { 
     if(CGRectIntersectsRect(v.frame, rect) || CGRectContainsRect(rect, v.frame)) { 
      [v drawRect:rect]; 
      NSLog(@"frame = %@",NSStringFromRect(v.frame)); 
     } 
    } 
    [super drawRect:rect]; 
} 
+0

그래서 완전히 다른 접근 방식으로이 문제를 해결할 수있었습니다. 추가 보너스로이 게시물은 나에게 '텀블 위드'배지를 받았습니다. 진지하게, 나는 누군가 자비를 베풀기를 바란다. .... – zer0gravitas

답변

1

당신은 요구하고있다. 사실상 당신은 텍스트를 그리고 코코아가 당신을 위해 텍스트를 그리는 것입니다. 그래서 super를 호출하지 마십시오.