Jorge Vega Sánchez
Jorge Vega Sánchez

Reputation: 7590

Change color of NSTextField

I'm trying to code an easy example to modify the color of a NSTextfield playing with green and red colors as good answer or wrong. I cannot achieve that cause i always obtained an error message when using this apple guide page for NSTextField, AppKit framework ref.

I'm trying to use this code :

@IBOutlet weak var mensajeResultado: NSTextField! 

when trying to colorize it without success
let rango = NSRange(location: 0,length: 0)
        mensajeResultado.superclass.setTextColor(NSColor.redColor(), range: rango)

Upvotes: 1

Views: 6977

Answers (2)

Eric Aya
Eric Aya

Reputation: 70119

Ken Thomases' answer is right, you just have to assign the color to the textColor property of your text field.

Just for info, it's very convenient to test in a Playground when you're not sure, for example:

import Cocoa
import XCPlayground

let tf = NSTextField(frame: NSMakeRect(50, 50, 100, 100))
tf.stringValue = "test"
tf.font = NSFont(name: "Helvetica", size: 16)
tf.backgroundColor = NSColor.blueColor()

tf.textColor = NSColor.whiteColor()

let v = NSView(frame: NSMakeRect(0, 0, 200, 200))
v.addSubview(tf)

XCPShowView("My View", v)

playground

Upvotes: 7

Ken Thomases
Ken Thomases

Reputation: 90701

Shouldn't it just be:

mensajeResultado.textColor = NSColor.redColor()

?

Upvotes: 8

Related Questions