gpt4 book ai didi

swift - NSTextField 边距和填充? ( swift )

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:46 24 4
gpt4 key购买 nike

我想知道是否可以为 NSTextField 设置边距或填充?

我实现了或多或少的自定义外观 textField(此屏幕截图中的第一个)...

enter image description here

...使用此代码:

myTextField.wantsLayer = true
myTextField.layer?.cornerRadius = 2.0
myTextField.layer?.borderWidth = 1.0
myTextField.layer?.borderColor = CGColor(red: 0.69, green: 0.69, blue: 0.69, alpha: 1.0)

但是,感觉我必须在左侧添加一些填充,以便数字不会太靠近边框。这可能吗?

最佳答案

没有简单明了的方法可以做到这一点,但就像 AppKit 中的许多事情一样,一旦您确切地弄清楚需要子类化的内容,这并不难。我遇到了this example by Hem Dutt在我对 macOS 10.12 的测试中,这种方法似乎运行良好。简而言之,您只需要子类化 NSTextFieldCell 并覆盖一些方法来更改文本框。这是 Swift 3 的一个变体:

class CustomTextFieldCell: NSTextFieldCell {

private static let padding = CGSize(width: 4.0, height: 2.0)

override func cellSize(forBounds rect: NSRect) -> NSSize {
var size = super.cellSize(forBounds: rect)
size.height += (CustomTextFieldCell.padding.height * 2)
return size
}

override func titleRect(forBounds rect: NSRect) -> NSRect {
return rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
}

override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
let insetRect = rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.edit(withFrame: insetRect, in: controlView, editor: textObj, delegate: delegate, event: event)
}

override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
let insetRect = rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.select(withFrame: insetRect, in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
}

override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
let insetRect = cellFrame.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.drawInterior(withFrame: insetRect, in: controlView)
}

}

我在原来的基础上做了一个显着的改变——覆盖 cellSize(forBounds:) 以增加单元格的最小高度。我正在使用自动布局来自动调整单元格的大小,因此如果没有覆盖,我的文本就会被剪裁。

关于swift - NSTextField 边距和填充? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39906160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com