3
votes

Bordure personnalisée sur NSTextField

Je souhaite personnaliser la bordure de NSTextFields. J'ai cherché et je suis assez sûr que cela doit être fait dans NSTextFieldCell dans drawInterior (withFrame: in :) , mais je ne sais pas comment.

Plus précisément, je veux une bordure inférieure uniquement, légèrement plus épaisse que la normale.


2 commentaires

Est-ce ce que vous recherchez stackoverflow.com/questions/38841251/set -border-for-nstextfi‌ champ ?


J’ai vu cela, mais si vous l’essayez et lisez les commentaires, cette approche pose toute une série d’autres problèmes.


3 Réponses :


0
votes

Vous pouvez ajouter à votre champ de texte une image d'arrière-plan et définir votre style de bordure aucun.


0 commentaires

3
votes

Vous devriez probablement lire la documentation de NSCell . Il dit que vous devez dessiner la bordure dans la fonction draw (withFrame: in) et vous devez appeler drawInterior (withFrame: in :) si vous remplacez draw ( withFrame: dans) . De plus, vous devez remplacer cellSize et renvoyer la taille appropriée qui prend en compte votre nouvelle bordure. J'ai mis à jour l'exemple avec la solution complète. Création d'un exemple de projet sur Github

/**
 Creates an custom border, that is just a line underneath the NSTextField.
 */
class CustomBorderTextFieldCell: NSTextFieldCell {
    // How thick should the border be
    let borderThickness: CGFloat = 3

    // Add extra height, to accomodate the underlined border, as the minimum required size for the NSTextField
    override var cellSize: NSSize {
        let originalSize = super.cellSize
        return NSSize(width: originalSize.width, height: originalSize.height + borderThickness)
    }

    // Render the custom border for the NSTextField
    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        // Area that covers the NSTextField itself. That is the total height minus our custom border size.
        let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - borderThickness)

        let path = NSBezierPath()
        path.lineWidth = borderThickness
        // Line width is at the center of the line.
        path.move(to: NSPoint(x: 0, y: cellFrame.height - (borderThickness / 2)))
        path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height - (borderThickness / 2)))
        NSColor.black.setStroke()
        path.stroke()

        // Pass in area minus the border thickness in the height
        drawInterior(withFrame: interiorFrame, in: controlView)
    }
}

Voici le résultat Bordure NSTextField personnalisée


0 commentaires

0
votes

Ajoutez à NSTableHeaderCell ce code

override func draw(withFrame cellFrame: NSRect,
                     in controlView: NSView) {
    let path = NSBezierPath()
    path.lineWidth = borderWidth
    // Line width is at the center of the line.
    path.move(to: NSPoint(x: cellFrame.minX, y: cellFrame.minY))
    path.line(to: NSPoint(x: cellFrame.maxX, y: cellFrame.minY))
    NSColor.black.setStroke()
    path.stroke()

    self.drawInterior(withFrame: cellFrame, in: controlView)
  }


0 commentaires