Hello Forum
First of all I want to thank you all for contributing to this platform, it was a great help while programming my application for the mac.
Now I’m stuck with something, maybe somebody can enlighten me.
I have a Textfield where I send some values to.
Now if the value exceeds a certain threshold, I want to highlight the background.
I found the following:
Related Methods: -[NSTextFieldCell setBackgroundColor:]
Then I was trying to follow this tutorial: http://macscripter.net/viewtopic.php?id=30373 however I get all the times errors.
When my code looks like in the tutorial:
tell class "NSColor" of current application
set backgroundColor to its colorWithCalibratedRed_green_blue_alpha_((243.0 / 255.0), (120.0 / 255.0), (31.0 / 255.0), 1)
end tell
textField's setBackgroundColor_(backgroundColor)
textField's setDrawsBackground_(true)
I get always the following error:
Unrecognized function setBackgroundColor_
I tried about thousand different things.
I think my problem is that I don’t want to press a button to change the color, I just want to change it when a certain value is exceeded, like:
if a>b then
setbackgroundcolor of textfield to red
end if
Anybody can point me out in which direction to go?
Thanks in advance
HI - If you place textField’s setDrawsBackground_(true) in the applicationWillFinishLaunching_(aNotification) delegate, this will work. I tested in Xcode 5.x under Mavrik and it worked.
This is what I advise you to do:
on applicationWillFinishLaunching_(aNotification)
textField’s setDrawsBackground_(true)
textField’s setBackgroundColor_(current application’s NSColor’s blackColor())
end applicationWillFinishLaunching_
or you can create a hander as follow and call it form the above:
on backGroundColor_(sender)
textField's setDrawsBackground_(true)
textField's setBackgroundColor_(current application's NSColor's blackColor())
end backColor_
on applicationWillFinishLaunching_(aNotification)
backGroundColor_(me)
end applicationWillFinishLaunching_
In MainMenu.xib, click on your textfield and in the “Show attribute inspector” check RefusesFirstResponder. this will give you what you want without errors.
One more thing, setDrawBackground: sets only the Bezel (the border). Sorry, I don’t quite remember this well since I did this over two years ago.
Are you sure your textField outlet is connected correctly? I’d like to see the actual error in your log, cut-and-pasted in full, but unrecognised function errors are usually either mis-typed method names or methods being sent to something other than what you think.
You are right. He probably did not connect textField in IB to the textField. I deleted the connection to the textField and received the following error.
-[AppDelegate applicationWillFinishLaunching:]: unrecognized function setBackgroundColor_. (error -10000)
if setting the text color instead of the background color is an alternative,
this can be quite easily accomplished with bindings
bind the value of the text field to textFieldValue
bind the text color of the text field to textFieldTextColor
property textFieldValue : ""
property valueThreshold : 10.0
on keyPathsForValuesAffectingTextFieldTextColor()
return current application's NSSet's setWithObject:"textFieldValue"
end keyPathsForValuesAffectingTextFieldTextColor
on textFieldTextColor()
if (textFieldValue as real) > valueThreshold then
return current application's NSColor's redColor()
else
return missing value
end if
end textFieldTextColor
Hi Shane, hi t524ube
Thanks again for helping me.
I’m sure i did not make the correct connections.
At the moment it looks like this:
The code I want to end up with is the following:
--Text to the textfield
set my offq1 to value1
if value1 > value2
--set background of textcell to red
end if
I’m somehow stuck with the ‘on applicationWillFinishLaunching_(aNotification)’ part - I thought this is for stuff which has only to do with the initialization of the program, why do I have to call the handler from there?
I saw right now StefanK’s reply, setting the Text color is another option if the background is too difficult for me.
Thanks again all
What you have there is the value of the text field (i.e., its text) bound to the variable. You want the variable to be an outlet for the text field. You must understand the distinction, or you will get no further.
Get rid of the binding, control-click on the app delegate, and drag from next to the variable across to the text field to connect it as an outlet.
You don’t have to call it from applicationWillFinishLaunching_ – that’s just somewhere to test it from.
I haven’t been able to draw a Color behind my TextFieldCell.
Somehow it just didn’t work.
I changed the outlet thing - and I learned a lot - thanks for the hints.
My solution:
I just placed a Color Well component behind the TextFieldCell and changed the color of that one if needed.
Maybe this idea helps somebody else sometimes.
Thanks again forum for the hints.
tell class "NSColor" of current application
--red
set rotcolorgewaehlt to its colorWithCalibratedRed:(255.0 / 255.0) green:(181.0 / 255.0) blue:(189.0 / 255.0) alpha:1
--backgroundcolor
set backcolorgewaehlt to its colorWithCalibratedRed:(237.0 / 255.0) green:(237.0 / 255.0) blue:(237.0 / 255.0) alpha:1
end tell
colorwell setColor:backcolorgewaehlt
I guess there are a couple of ways to do this with a TextField, I think that in order to get to the text field’s cell’s background color.
What is important here, is that a TextField, is a “combo-object”, that consists of both a TextField, and a TextFieldCell.
It is organized this way, so that you have just one editor for all your TextFields in one window I guess.
Well. To set the background color you’d use something like NSTextField’s cell’s setBackgroundColor.
The other thing is that you can call this from a ValueTransformer, which is at least work with Objective-C, that you configure your TextField to work with.
At least I hope this gives you some pointers in the right direction.