I have an NSTextField
that is used as either a label or an editable text field depending on whether the application is in edit mode or in view mode.
The code to change the editability of the text field is:
var editable : Bool = false {
didSet {
if editable {
titleTextField?.editable = true
titleTextField?.bezeled = true
titleTextField?.drawsBackground = true
titleTextField?.cell?.usesSingleLineMode = false
titleTextField?.cell?.wraps = true
editButton?.state = NSOnState
} else {
titleTextField?.editable = false
titleTextField?.bezeled = false
titleTextField?.drawsBackground = false
titleTextField?.cell?.usesSingleLineMode = false
titleTextField?.cell?.wraps = true
editButton?.state = NSOffState
}
}
}
@IBAction func editButtonPushed(sender : NSButton) {
if sender.state == NSOnState {
self.editable = true
}else{
self.editable = false
}
}
I've defined the NSTextField
in Interface Builder as editable and 'Uses Single Line Mode' is not selected. The text field is in a custom view which is the right view in a NSSplitView
. The custom view also contains a button with which the user can toggle the edit mode (it is connected to the action in the above code).
When the title in the text field is long (i.e. more than one line) the title is displayed correctly in the editable text field. When I click the Edit button (turning of Edit mode) the text field is made non-editable (the else statement above).
The problem is that in non-edit mode the NSTextField
(and the parent custom view) resizes so that the full title fits into the NSTextField
(now a label) on ONE LINE. I want the text field to remain the same size and use two lines to display the title.
How can I prevent the resizing of the text field? I've tried using titleTextField?.cell?.usesSingleLineMode = false
and titleTextField?.cell?.wraps = true
, but neither solution works.