gpt4 book ai didi

macos - NSTextView 双视

转载 作者:行者123 更新时间:2023-12-03 17:13:03 25 4
gpt4 key购买 nike

我一定是做错了什么: textview with "double vision"

我的 Cocoa 应用程序有一个围绕自定义 View 的 ScrollView ,而自定义 View 又具有一个 TextView 。我只希望看到一个“这是一个”字符串,但角落里还有一个额外的字符串。我已将代码减少到非常少,但仍然不明白我的错误是什么,所以我在这里寻找线索。

下面是自定义 View 的 View Controller ,但为了简单起见,这里有一个指向 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];
}

最佳答案

您正在调用:

[super drawRect:rect];

并且您正在绘制函数中自己绘制文本。实际上,您正在绘制文本, cocoa 也正在为您绘制文本。所以不要调用 super。

关于macos - NSTextView 双视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15390634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com