nstextfield

Subclassing an NSTextField

家住魔仙堡 提交于 2019-12-02 05:32:23
问题 Given all the complex things I seem to cover every day, this appears to be a "what the heck am I doing wrong that seems to simple?" scenario! I would like to subclass an NSTextField to change the background color and text color. For simplicity sake (and to help anyone who hasn't ever subclassed anything before), here is the example of my (simplified) subclass MyNSTextFieldSubclass ... Step 1: Create the subclass file: First the header file @interface MyTextFieldSubclass : NSTextField { } @end

Truncate beginning of label

血红的双手。 提交于 2019-12-02 05:08:11
I have a label ( NSTextField ) with text aligned right. When the text becomes too large to display, the end of the text is truncated (at the right edge of the label). In my case I would actually want to truncate the beginning of the text. How can I achieve this in the Interface Builder or in code? Now | This sentence is to | Desired | entence is too long | I'm not quite sure if this works for NSTextFields, I know it does for UILabels, but have you tried : myTextField.lineBreakMode = NSLineBreakMode.ByTruncatingHead What you're looking for is [[myTextField cell] setLineBreakMode:

Recognize if user has pressed arrow key while editing NSTextField swift

馋奶兔 提交于 2019-12-02 04:02:59
I have many NSTextFields and I want to know, if the user has pressed one of the arrow keys while editing one of them. The function override func keyDown(theEvent: NSEvent) { switch theEvent.character { case NSRightArrowFunctionKey: println(1) moveGor(NSRightArrowFunctionKey) case NSLeftArrowFunctionKey: moveGor(NSLeftArrowFunctionKey) case NSUpArrowFunctionKey: moveVert(NSUpArrowFunctionKey) case NSDownArrowFunctionKey: moveVert(NSDownArrowFunctionKey) default: super.keyDown(theEvent) } } doesn't seem to work. Is there any other way to do that in swift? EDIT: I have the extension for NSEvent :

Multi-line NSTextField not working

浪尽此生 提交于 2019-12-02 00:03:00
问题 I have been trying to get a multi-line NSTextField to lay out automatically using preferredMaxLayoutWidth . I can’t figure out why this doesn’t work. class ViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() let textField = NSTextField() textField.cell!.usesSingleLineMode = false textField.cell!.wraps = true textField.cell!.lineBreakMode = .byCharWrapping view.addSubview(textField) textField.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint

How to live check a NSTextField - Swift OS X

假如想象 提交于 2019-12-01 19:26:43
I am currently making an OS X application written in Swift. What I want to do is when the user enters text in a NSTextField , I want to run a function that checks the value and adds it to a Label. How would I do this in swift? Conforms ViewController to protocol NSTextDelegate . Assign ViewController as Delegate for TextField . Implement controlTextDidChange method. import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var textField: NSTextField! @IBOutlet weak var label: NSTextField! func

-[NSTextField intrinsicContentSize] always has undefined width

血红的双手。 提交于 2019-12-01 17:18:32
I'm pulling hairs here. I have an NSTextField created on a xib that has an intrinsicContentSize adjusted to it's stringValue. If I create a textField programmatically like so: _textfield = [[NSTextField alloc] initWithFrame:CGRectZero]; _textfield.translatesAutoresizingMaskIntoConstraints = NO; _textfield.alignment = NSCenterTextAlignment; _textfield.drawsBackground = NO; [_textfield setBordered:NO]; _textfiled.stringValue = @"Test" It's intrinsicContentSize is always (width=-1, height=16) I've tried calling invalidateIntrinsicContentSize but to no avail… How does one get NSTextField's

NSTextFieldCell's cellSizeForBounds: doesn't match wrapping behavior?

╄→гoц情女王★ 提交于 2019-12-01 14:01:57
It seems to be commonly accepted that cellSizeForBounds: allows one to calculate a text field's "natural" size. However, for NSTextField, I've found that it doesn't quite match: @interface MyTextField : NSTextField @end @implementation MyTextField - (void)textDidChange:(NSNotification *)notification { [super textDidChange:notification]; [self validateEditing]; // Forces updating from the field editor NSSize cellSize = [self.cell cellSizeForBounds: NSMakeRect(0, 0, self.bounds.size.width, CGFLOAT_MAX)]; NSRect frame = self.frame; CGFloat heightDelta = cellSize.height - frame.size.height; frame

NSTextField placeholder text doesn't show unless editing

て烟熏妆下的殇ゞ 提交于 2019-12-01 13:54:23
问题 I set the placeholder text of my NSTextField in Interface Builder, but the placeholder text doesn't show until I click inside the text field to edit it. Anyone else having this issue? Thanks EDIT: Screenshots. Here's what the field looks like when I'm not editing it: Not editing http://cl.ly/2a0R1h1p2A082g1t1b3q/Screen_Shot_2011-04-01_at_6.51.25_PM.png And this is what it looks like when I'm editing it: Editing http://cl.ly/1c2d1U233R3j3k0M2E2Q/Screen_Shot_2011-04-01_at_6.51.32_PM.png 回答1:

Obj-C: Why is my NSTextField subclass having an effect on objects I haven't assigned to it?

岁酱吖の 提交于 2019-12-01 10:35:05
问题 I have an NSPanel with about 4 different NSTextField's on it. I'm looking at getting the usual cut, copy, paste, selectAll working for some of the fields. The best solution I've found appears to be here: http://web.archive.org/web/20100126000339/http://www.cocoarocket.com/articles/copypaste.html The AXCVHandler becomes a subclass of NSTextField . On my NSPanel in interface builder if I change a single NSTextField's class to be AXCVHandler, as shown in the link above, then the expected

How to do something when Enter-key is pressed on NSTextField

自古美人都是妖i 提交于 2019-12-01 10:02:10
问题 I'm writing an app for the Mac using Swift. I write a string inside a NSTextField object and I would like to save it in a .txt file. I would like that to happen as soon as the user presses Enter-key. My method .writeToFile() is ready, I cannot figure out how to make it run as soon as that key is pressed. I could not find a proper answer for Swift language. Thank you. 回答1: @IBAction func textFieldAction(sender: NSTextField) { print(sender.stringValue) } 来源: https://stackoverflow.com/questions