NSTextField, Change text in Swift
NSTextField is different from a UITextField. It doesn't have a text
property. It does however inherit from NSControl which has a stringValue property.
sumlab.stringValue = "\(result.sum)"
Change the text in a NSTextField programmatically?
This will work:
[field setStringValue:[NSString stringWithFormat:@"%i",number]];
If it doesn't do anything, it probably means field
isn't set to point to the field (e.g. by making a connection in Interface Builder).
Observe text change in NSTextView
Just set the text view's delegate. The delegate must coform to NSTextViewDelegate
. NSTextViewDelegate
inherits from NSTextDelegate
(see relationships and NSTextView
is a subclass of NSText
). NSTextDelegate
defines method
optional func textDidChange(_ notification: Notification)
Text change notification for an NSTextField
If you just want to detect when the value of a text field has changed, you can use the controlTextDidChange:
delegate method that NSTextField
inherits from NSControl
.
Just connect the delegate
outlet of the NSTextField
in the nib file to your controller class, and implement something like this:
- (void)controlTextDidChange:(NSNotification *)notification {
NSTextField *textField = [notification object];
NSLog(@"controlTextDidChange: stringValue == %@", [textField stringValue]);
}
If you're creating the NSTextField
programmatically, you can use NSTextField
's setDelegate: method after creation to specify the delegate:
NSTextField *textField = [[[NSTextField alloc] initWithFrame:someRect] autorelease];
[textField setDelegate:self]; // or whatever object you want
Delegation is one of the fundamental design patterns used throughout Cocoa. Briefly, it allows you to easily customize the behavior of standard objects (in this case, user interface objects) without the complexity involved in having to subclass the object to add that additional behavior. For example, another lower-level way to detect when the text in a textfield has changed might be to create your own custom NSTextField
subclass in which you override the keyDown:
method that NSTextField
inherits from NSResponder
. However, subclassing like that is difficult because it can require that you have an intimate knowledge of the object's inheritance hierarchy. For more info, definitely check out the following:
Cocoa Fundamentals Guide: Delegates and Data Sources
Regarding what id <NSTextFieldDelegate>
means: it means a generic object (id
) that declares itself as conforming to the <NSTextFieldDelegate>
protocol. For more info on protocols, see The Objective-C Programming Language: Protocols.
Sample GitHub project at: https://github.com/NSGod/MDControlTextDidChange
Change NSTextField text color with fade animation - Cocoa
Here is the outline of one possible solution:
- Subclass
NSAnimation
, say withTextColorAnimation
- Have the
init
take theNSTextField
and finalNSColor
- Override
currentProgress
as perNSAnimation
docs to (a) call the super implementation and (b) set the intermediate color anddisplay
theNSTextField
- Use
NSColor.blend(...)
to determine the intermediate color start
thisNSAnimation
You should get a nice smooth color transition. HTH
How to Change NSTextField Font Size When Used in SwiftUI
This is a particularly weird one:
struct FancyTextField: NSViewRepresentable {
@Binding var text: String
func makeNSView(context: Context) -> NSTextField {
let textField = MyNSTextField()
textField.customSetFont(font: .systemFont(ofSize: 50))
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
}
}
class MyNSTextField : NSTextField {
func customSetFont(font: NSFont?) {
super.font = font
}
override var font: NSFont? {
get {
return super.font
}
set {}
}
}
Maybe someone will come up with a cleaner solution to this, but you're right -- the normal methods for just setting .font
on the NSTextField do not seem to work here. It seems to be because outside elements (the debugger doesn't give me a good hint) are trying to set the font to system font size 13.
So, my solution is to subclass NSTextField
and make the setter for font
not responsive. Then, I define a custom setter method, so the only way to get up to the real setter is through my custom method.
A little hacky, but it works.
How to change insertion text (cursor) for NStextField? for os x swift 2
Never mind found the answer by extending NSTextField class and calling the function manually
extension NSTextField {
public func customizeCaretColor(caretColor: NSColor) {
let fieldEditor = self.window?.fieldEditor(true, forObject: self) as! NSTextView
fieldEditor.insertionPointColor = caretColor
}
}
Related Topics
Checking If Textfields Are Empty Swift
Swift Http Request Use Urlsession
Compare Textfield.Text to Firebase String Swift
Default Argument Not Permitted in a Tuple Type When Defining Function Type
Nsurl Found Nil While Unwraping an Optional Value
How to Cast Any to an Optional
Format Realtime Stopwatch Timer to the Hundredth Using Swift
Higher Order Function: "Cannot Invoke 'Map' with an Argument List of Type '((_) -> _)'"
Creating a Countableclosedrange<Character>
Non-Strong References Not Working in Playground
Why Does Swift Playground Shows Wrong Number of Executions
How to Make Physics Bodies Stick to Nodes Anchor Points
What Does "Get" Mean in a Protocol's Property Declaration
Declaration Is Only Valid at File Scope (Extension)
Swift: How to Delete Part of Audio