I am just starting out in xCode and I have built a very simple (mac, not iPhone) app that pops up a window with a text field, a label and a button.
I can get the app to pop up a message box with the string value of the text box, but I can't seem to set the string value of the text field with some default text.
The code I am using to try to populate the text field is:
[_txtBox setStringValue:#"Hello World"];
the set string method is the only method that looks like it should set the value of the box. The program runs with no errors and when I click on the button, no error's are thrown.
If the text field contains text, the text displays in the label as it should.
If I place the line of code above just before the code that reads and outputs the text field, then instead of the text box showing that text box, the text box text just disappears as if I had over written the contents of the field with an empty string. The label is also blank.
Suggestionsn would be welcome; the almost identical expression I am using on the label works fine:
[_lblOutPut setStringValue:_txtIP];
So I have no idea what I am doing wrong...
Cheers!
Dave
Check whether you are using "NSTextField with NumberFormatter" or "NSTextField".
"NSTextField with NumberFormatter" allows only numerals. If you try to set NSString it will display only an empty string.
Try this
[_txtBox setStringValue:#"123"];
Now, If you are getting 123 in your output, then definitely you are using "NSTextField with NumberFormatter"
For text input, are you using a NSTextField or an NSTextView? They are vastly different in handling, setStringValue: works for NSTextField, yet NSTextView requires an attributed string to be passed to [[textView textStorage] setAttributedString:...].
Are you sure you connected the right way the outlet in IB?
what's the output of
NSLog(#"%#", _txtBox);
(put it before the setStringValue)
Related
I would like to create an NSMenuItem with more than one line of text in it, and I'd also like an icon to appear next to both lines of text. I can't use a custom view, because then it wouldn't highlight correctly, and I can't just put a newline in the menu item's title, because the newline gets turned into a space. What should I do?
It turns out that although you can't put a newline in the title property of a menu item, you can put a newline in the attributedTitle. Something like this will work:
item.attributedTitle = [[NSAttributedString alloc] initWithString:#"line 1\nline 2"];
I mean, NSTextFields have attributted text properties, right? So, what is the purpose of this "rich text" option on interface builder for NSTextFields on cocoa?
As usual, no documentation about it.
From the docs:
If YES, and the text value is an attributed string, it is displayed
using the attributed string’s visual settings, which can be modified
in the font panel.; if flag is NO and the text is an attributed
string. the string attributes (font, color, etc.) are ignored and the
string is displayed based on the text field’s settings. Setting the
attributed string’s attributes are ignored when displaying the string
and when the text field is editing.
Actually, the way it appears to work if RichText is disabled with regards to the Font Panel is that the Font, Color, etc. is actually changed - just for the whole text, not just the selected section/characters...
When I set the multiline property of the text control to true, when in use, if the text control is used as a basic one line textbox and the text typed in goes past the length of the box, it only highlights text up the end of the box, you have to press the right arrow key to highlight the rest. I need the multiline property set to true because in some scenarios it is necessary. I've thought of creating the text control twice with the property turned off in one and on in the other but wanted to know if anyone knew a solution?
I am using Table View Control. In the control, I show File/Folders details, if I reduce the size of file/folder name column, it does not show partial file/folder name (whatever characters can be shown in the Column).
If a file name has multiple words in it, if enough space is not there to show a word then that word does not get shown at all, instead of showing some characters in that word (Either a word gets shown completely, or not at all)
I want to know how to resolve this issue.
Thanks so much.
Set the line break mode of the column's text cell, using either the Attributes inspector in IB or a setLineBreakMode: message to the cell.
I am creating a Status Bar application which requires a URL being entered in a text field. The text field is a subview of the NSMenuItem. The problem I am facing though is that the value of the text field cannot be changed and only "indirectly highlighted" (it's not the normal blue highlighting but a grey one and only occurs when double clicking the value of the text field).
Does someone know why this is?The text field hasn't been disabled by me, but it seems to apply read only properties or something...
It's most likely your menu has "Auto Enables Items" turned on. With this on (the default), your menu items have to have an action and a target to be enabled, which is likely disabling their contents (your NSTextField). Turn it off and you should be fine.