首页 > 解决方案 > 为什么我看不到 NStextView 的内容

问题描述

在使用objectivec开发MAC app的时候,遇到一个很奇怪的问题。我在本地调试的时候是用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;
}

添加到VC

- (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];
}

更新用户界面

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);

标签: objective-cmacos

解决方案


我发现了原因。我在添加视图时注意到了顺序,所以我在本地运行是正常的。但是在发布的时候,后面的背景居然跑到了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];
}

推荐阅读