objective-c - 为什么我看不到 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);
解决方案
我发现了原因。我在添加视图时注意到了顺序,所以我在本地运行是正常的。但是在发布的时候,后面的背景居然跑到了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];
}
推荐阅读
- ios - 不同颜色的标题区域
- image - 对使用图像数据生成器设置 steps_per_epoch 感到困惑
- java - Java 命令行计算器帮助。添加数字序列
- node.js - 如何解决这个“SyntaxError: Invalid regular expression: /^function authenticate(req, res, next)”?
- javascript - DataTable 没有正确过滤新插入的数据
- scipy - 在计算 Spearmans 等级相关性(scipy)之前,我应该对我的序数变量进行编码吗?
- android - 测试中 delay() 与 withTimeout() 协程之间的区别
- python - 有没有办法存储我的程序所做的所有 GET 请求的值?
- c# - 编译器问题,努力使用 Dispatcher.RunAsync() 调用 UI 线程并返回一个值
- flutter - 如何在 Flutter 中设置 CupertinoTextField 边框颜色?