为什么我看不到NStextView

时间:2020-07-02 07:45:55

标签: objective-c macos

当我使用Objectivec开发MAC应用程序时,遇到了一个非常奇怪的问题。在本地调试时,我使用nstextview设置文本,但是在发布时,看不到文本,但是它在那里。即使鼠标移动到那里,它也可以变成文本输入状态。右键单击也可以搜索nstextview内容。如何调查这个问题? 吸气剂:

- (NSScrollView *)broadcastTextViewScrollView
{
    if(!_broadcastTextViewScrollView)
    {
        _broadcastTextViewScrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 400, 100)];
        [_broadcastTextViewScrollView setHasVerticalScroller:YES];
        [_broadcastTextViewScrollView setHasHorizontalScroller:NO];
        [_broadcastTextViewScrollView setBorderType:NSNoBorder];
        [_broadcastTextViewScrollView setDrawsBackground:NO];
        [_broadcastTextViewScrollView setHorizontalScrollElasticity:NSScrollElasticityNone];
        _broadcastTextViewScrollView.documentView = self.broadcastTextView;
    }
    return _broadcastTextViewScrollView;
}

- (NSTextView *)broadcastTextView
{
    if(!_broadcastTextView)
    {
        _broadcastTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(2 * fMargin, CGRectGetMinY(self.broadcastBackgroundView.frame), NSWidth(self.view.frame) - 4 * fMargin, fMinBackgroundHeight)];
        _broadcastTextView.editable = NO;
        _broadcastTextView.backgroundColor = [NSColor clearColor];
        _broadcastTextView.verticallyResizable = YES;
        _broadcastTextView.textContainer.heightTracksTextView = YES;
        _broadcastTextView.textContainer.widthTracksTextView = YES;
        _broadcastTextView.focusRingType = NSFocusRingTypeNone;
        _broadcastTextView.selectable = NO;
        _broadcastTextView.drawsBackground = NO;
    }
    return _broadcastTextView;
}

addtoVC

- (void)configureTextView
{
    [self.view addSubview:self.broadcastBackgroundView];
    [self.view addSubview:self.broadcastTextViewScrollView];
}

vc在NSWindow上

- (void)confugureBroadcastAttendeeWindow
{
    int styleMask = 1;
    [self.boBroadcastAttendeeWindowController.broadcastWindow setWindowStyleMask:styleMask];
    [self.boBroadcastAttendeeWindowController.broadcastWindow setContentSize:self.boBroadcastAttendeeViewController.view.frame.size];
    [self.boBroadcastAttendeeWindowController.broadcastWindow setClientView:self.boBroadcastAttendeeViewController.view];
}

updateUI

NSDictionary* params = @{NSFontAttributeName:[NSFont systemFontOfSize:14],NSForegroundColorAttributeName:[NSColor colorWithHexRGB:kColorBlack_121212 andAlpha:1]};
    NSString* addtionString;
    if(self.broadcastTextView.string.length)
    {
        addtionString = [NSString stringWithFormat:@"%@\n%@",self.broadcastTextView.string,self.message];
    }
    else
    {
        addtionString = self.message;
    }
    NSAttributedString* attrString = [[NSAttributedString alloc] initWithString:addtionString attributes:params];
    
    [self.broadcastTextView.textStorage setAttributedString:attrString];
    
    CGFloat textViewMaxWidth = NSWidth(self.broadcastBackgroundView.frame) - 2 * fMargin;
    CGFloat textViewHeight = [[attrString string] boundingRectWithSize:NSMakeSize(textViewMaxWidth, fMaxBroadcastTextViewHeight) options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:params].size.height;
    if(textViewHeight < fMinBackgroundHeight)
    {
        textViewHeight = fMinBackgroundHeight;
    }
    if(textViewHeight > fMaxBackgroundHeight)
    {
        textViewHeight = fMaxBackgroundHeight;
    }
    CGFloat borderMinY = NSMaxY(self.okButton.frame) + fMargin;
    CGFloat textViewMarginH = fMargin + fTextViewMarginV;
    self.broadcastTextViewScrollView.frame = NSMakeRect(textViewMarginH, borderMinY, NSWidth(self.view.frame) - 2 * textViewMarginH, textViewHeight);
    self.broadcastTextView.frame = NSMakeRect(0, 0, NSWidth(self.broadcastBackgroundView.frame), NSHeight(self.broadcastBackgroundView.frame));
    self.broadcastBackgroundView.frame = NSMakeRect(fMargin, borderMinY - fTextViewMarginV, NSWidth(self.view.frame) - 2 * fMargin, NSHeight(self.broadcastTextViewScrollView.frame) + 2 * fTextViewMarginV);
    self.fromTextField.frame = NSMakeRect(fMargin, NSMaxY(self.broadcastBackgroundView.frame) + fTextFieldBottom, NSWidth(self.broadcastBackgroundView.frame), fTextFieldHeight);
    self.titleTextField.frame = NSMakeRect(fMargin, NSMaxY(self.fromTextField.frame) + fTextFieldBottom, NSWidth(self.broadcastBackgroundView.frame), fTextFieldHeight);
    self.broadcastTextView.maxSize = NSMakeSize(NSWidth(self.broadcastTextView.frame), textViewHeight);

1 个答案:

答案 0 :(得分:0)

我发现了原因。添加视图时,我注意到了顺序,所以在本地运行是正常的。但是,在发行过程中,背面的背景实际上到达了textview的顶部,因此被覆盖了。现在,我更改视图的所有权。

之前

- (void)configureTextView
{
    [self.view addSubview:self.broadcastBackgroundView];
    [self.view addSubview:self.broadcastTextViewScrollView];
}

之后

- (void)configureTextView
{
    [self.view addSubview:self.broadcastBackgroundView];
    [self.broadcastBackgroundView addSubview:self.broadcastTextViewScrollView];
}
相关问题