自定义绘图顶部的 NSTextField - 黑色轮廓和光标不闪烁?
我完全被这个问题难住了。我制作了一个使用几个不同类的自定义搜索控件。由于某种原因,当 NSTextField 位于这些不同部分的任何位置时,它会在其周围显示实心黑色边框,并且光标不会闪烁。
如果有人有几分钟的时间 - 我已经将我的代码放在了 Pastebin 上。
这是搜索控件的图片,以及在这种特殊情况下的外观:
搜索控件位于渐变视图的顶部:
http://pastebin.com/m43fde2b6
搜索控件由以下代码拼凑而成:
http://pastebin.com/m5be08c32
搜索控件的实际图形部分由两个类构建:< br> http://pastebin.com/m5bfa9439
http://pastebin.com/m5e909a2f (扩展至类之上)
我找不到到底是什么错误的。文本有效,但有黑色边框,并且光标不闪烁。我做错了什么?
呃,我已经为这个问题抓了好几天了。
I'm completely stumped with this problem. I made a custom search control that uses a few different classes. For some reason, when an NSTextField is anywhere over these different pieces, it displays a solid black border around it, and the cursor doesn't blink.
If anyone has a couple minutes - I've put together my code on pastebin.
Here's a picture of the search control, and what it looks like in this particular case:
The search control is sitting on top of a gradient view:
http://pastebin.com/m43fde2b6
The search control is pieced together with this code:
http://pastebin.com/m5be08c32
The actual graphical part of the search control is built from two classes:
http://pastebin.com/m5bfa9439
http://pastebin.com/m5e909a2f (extends above class)
I cannot find what the heck is wrong. The text works, but there's a black border, and the cursor doesn't blink. What am I doing wrong?
Arg, I've been pulling my hair out for days on this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mac OS X 中从未很好地支持将一个视图置于同级视图上。请尝试将其设为子视图。您甚至可能希望将其作为专用搜索字段视图的私有组件。
关于这一点,您不使用 NSSearchField 是否有原因?
Putting one view over a sibling view has never been well-supported in Mac OS X. Try making it a subview instead. You may even want to make it a private component of a dedicated search-field view.
On that note, is there a reason you're not using NSSearchField?
我明白了!最后。
我没有意识到“drawRect:”方法的参数“dirtyRect”是控件中“脏”的部分,这意味着它需要重新绘制。
因此,当 NSTextField 位于控件顶部时,它将触发该控件的“drawRect:”被调用 (3) 不同时间 - 使用不同的“dirtyRect”参数。
1:光标 - 通常是 NSMakeRect(textField.origin.x,textfield.origin.y,1,textfield.origin.height)。
2:文本字段框架
3:文本字段所在控件的大小。
因此,解决方法很简单,将控件的比例 9 绘图更改为始终绘制到 [自身框架]。您可以在此处查看更改:http://pastebin.com/m50a5b0ad(第 89 行)。
以前,它绘制到“rect”参数(http://pastebin.com/m5e909a2f - 第 88 行),但根据 drawRect 调用的来源,矩形参数的大小也不同。
I got it figured out! Finally.
What I didn't realize was the the "drawRect:" method's parameter "dirtyRect", is the portion of the control that is "dirty", meaning it needs to be redrawn.
So, when an NSTextField is on top of a control, it will trigger that control's "drawRect:" to be called (3) different times - with different "dirtyRect" parameters.
1: the cursor - usually an NSMakeRect(textField.origin.x,textfield.origin.y,1,textfield.origin.height).
2: the text field frame
3: the size of the control the text field is sitting on.
So, the fix was simple, change my control's scale 9 drawing to always draw to [self frame]. You can see the change here: http://pastebin.com/m50a5b0ad (line 89).
Previously, it was drawing to the "rect" parameter (http://pastebin.com/m5e909a2f - line 88), but depending on where the drawRect call was coming from, the rect parameter was different sizes.