Skip to content

Instantly share code, notes, and snippets.

@eonist
Created May 21, 2023 13:42
Show Gist options
  • Save eonist/ebd31d4485aea09d5dfced04b2926d8d to your computer and use it in GitHub Desktop.
Save eonist/ebd31d4485aea09d5dfced04b2926d8d to your computer and use it in GitHub Desktop.
CustomTextFieldCell.swift
#if os(macOS)
import Cocoa
/**
* Adds custom padding
* - Remark: ⚠️️ Experimental, not yet in use ⚠️️ deprecate? move to issues or gist?
*/
final class CustomTextFieldCell: NSTextFieldCell {
private static let padding: CGSize = .init(width: 2.0, height: 4.0)
/**
* - Parameter rect: NSRect
*/
override func cellSize(forBounds rect: NSRect) -> NSSize {
var size = super.cellSize(forBounds: rect)
size.height += (CustomTextFieldCell.padding.height * 2)
return size
}
/**
* - Fixme: ⚠️️ doc
* - Parameter rect: - Fixme: ⚠️️
* - Returns: - Fixme: ⚠️️
*/
override func titleRect(forBounds rect: NSRect) -> NSRect {
rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
}
/**
* - Fixme: ⚠️️ doc
* - Parameters:
* - rect: - Fixme: ⚠️️
* - controlView: - Fixme: ⚠️️
* - textObj: - Fixme: ⚠️️
* - delegate: - Fixme: ⚠️️
* - event: - Fixme: ⚠️️
*/
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)
}
/**
* - Fixme: ⚠️️ doc
* - Parameters:
* - rect: - Fixme: ⚠️️
* - controlView: - Fixme: ⚠️️
* - textObj: - Fixme: ⚠️️
* - delegate: - Fixme: ⚠️️
* - selStart: - Fixme: ⚠️️
* - selLength: - Fixme: ⚠️️
*/
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)
}
/**
* - Fixme: ⚠️️ doc
* - Parameters:
* - cellFrame: - Fixme: ⚠️️
* - controlView: - Fixme: ⚠️️
*/
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)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment