简体   繁体   English

更改NSTextField的颜色

[英]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. 我正在尝试编写一个简单的示例,以修改NSTextfield的颜色,将绿色和红色用作正确答案或错误。 I cannot achieve that cause i always obtained an error message when using this apple guide page for NSTextField, AppKit framework ref. 我无法实现该原因,当我将此苹果指南页面用于NSTextField, AppKit框架参考时,总是会收到错误消息

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)

Ken Thomases' answer is right , you just have to assign the color to the textColor property of your text field. Ken Thomases的答案是正确的 ,您只需要将颜色分配给文本字段的textColor属性即可。

Just for info, it's very convenient to test in a Playground when you're not sure, for example: 仅出于信息目的,在不确定时在Playground中进行测试非常方便,例如:

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)

操场

Shouldn't it just be: 不应该只是:

mensajeResultado.textColor = NSColor.redColor()

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM