2013-03-13 73 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]; 

和您绘制自己的文字在你的绘制函数。 实际上,您正在绘制文字,可可正在为您绘制文字。 所以不要叫超级。