2013-03-13 90 views
1

我必須錯在這裏做的事情: textview with "double vision"NSTextView複視

我的可可應用程序大約有這反過來又一個TextView自定義視圖滾動型。我只希望看到一個「這是一個」字符串,但角落裏還有一個額外的字符串。 我已經減少了代碼,使其非常小,仍然不明白我的錯誤是什麼,所以在這裏我正在尋找線索。

自定義視圖的視圖控制器如下,但爲簡單起見,此處鏈接到project

#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 2013-03-20 17:32:53

回答

1

要調用:

[super drawRect:rect]; 

和您繪製自己的文字在你的繪製函數。 實際上,您正在繪製文字,可可正在爲您繪製文字。 所以不要叫超級。