Swift NSTextField文本消失 [英] Swift NSTextField text disappears

查看:54
本文介绍了Swift NSTextField文本消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能存在于Objective-c的6岁职位上.我没有找到最近有效的答案或问题,也没有发现用Swift写的答案.

This questions MIGHT exist on a 6 year old post in objective-c. I haven't found a recent answer or question that works, or one that is written in Swift.

我正在使用情节提要,并且已将 NSTextField 子类化.由于某些原因,当我单击该字段时,将显示占位符,并且当我输入文本并单击时,文本会消失.文本实际上就在那儿,因为当我再次单击时,输入的文本会保留下来.图层掩盖了文本值似乎是某种渲染问题?很奇怪.我正在使用 NSTrackingArea ,还添加了 CALayer .

I'm using storyboards and I have subclassed NSTextField. For some reason when I click into the field, the placeholder shows and when I enter text and click out, the text disappears. The text is actually there, because when I click back in, the entered text persists. It seems that it's some type of rendering issue that a layer is covering up the text value? Very strange. I'm using NSTrackingArea and also adding CALayer.

以下是发生的情况的屏幕截图:

Here's a screenshot of what happens:

这是我的代码:

class NowTextField: NSTextField {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // required nonsense
        let textFieldLayer = CALayer()
        let textFieldRect = NSRect(x: 0, y: 0, width: 120, height: 32)
        let textFieldTrackingArea = NSTrackingArea.init(rect: textFieldRect, options: [NSTrackingAreaOptions.activeInActiveApp, NSTrackingAreaOptions.mouseEnteredAndExited], owner: self, userInfo: nil)
        self.wantsLayer = true
        self.layer = textFieldLayer
        self.addTrackingArea(textFieldTrackingArea)

        // styling
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.lightGray.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }

    override func mouseEntered(with event: NSEvent) {
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.highlightBlue.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }

    override func mouseExited(with event: NSEvent) {
        self.textColor = NSColor.darkGray
        self.layer?.cornerRadius = 4
        self.layer?.backgroundColor = NSColor.white.cgColor
        self.layer?.borderColor = NSColor.lightGray.cgColor
        self.layer?.borderWidth = 2
        self.drawsBackground = false
    }
}

推荐答案

我找到了答案.我删除了 let textFieldLayer = CALayer()以及 self.layer = textFieldLayer ,因为这些似乎在文本字段的顶部添加了不必要的层.这是工作版本的完整代码:

I found the answer. I deleted let textFieldLayer = CALayer() as well as self.layer = textFieldLayer as these seemed to be adding an unnecessary layer on top of the text field. Here's my full code for the working version:

class NowTextField: NSTextField {

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)

    // required nonsense
    let textFieldRect = NSRect(x: 0, y: 0, width: 120, height: 32)
    let textFieldTrackingArea = NSTrackingArea.init(rect: textFieldRect, options: [NSTrackingAreaOptions.activeInActiveApp, NSTrackingAreaOptions.mouseEnteredAndExited], owner: self, userInfo: nil)
    self.wantsLayer = true
    self.addTrackingArea(textFieldTrackingArea)

    // styling
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.lightGray.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

override func mouseEntered(with event: NSEvent) {
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.highlightBlue.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

override func mouseExited(with event: NSEvent) {
    self.textColor = NSColor.darkGray
    self.layer?.cornerRadius = 4
    self.layer?.backgroundColor = NSColor.white.cgColor
    self.layer?.borderColor = NSColor.lightGray.cgColor
    self.layer?.borderWidth = 2
    self.drawsBackground = false
}

}

这篇关于Swift NSTextField文本消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆