1

私はここで何か間違ったことをしているに違いありません: textview with

私のCocoaアプリには、カスタムビューの周りにスクロールビューがあり、カスタムビューにはテキストビューがあります。「これは」文字列が1つだけ表示されることを期待していますが、隅に余分な文字列があります。私はコードを非常に最小限に減らしましたが、それでも私のエラーが何であるかを理解していないので、ここで私は手がかりを探しています。

カスタムビューのビューコントローラは次のとおりですが、簡単にするために、ここにプロジェクトへのリンクがあります。

#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];
}
4

1 に答える 1