views:

160

answers:

1

What I am looking to do is change the default font of a text field and give it some padding so that when a person starts typing they will start typing further in from the edge of the text field.

Sorry, If my question is quite vague, i don't mean to be, if you need some more information just comment.

+2  A: 

In answer to your first question, you can change the font of an NSTextField by using the setFont:size: method (since NSTextField inherits from NSControl - docs here):

NSTextField *textField;     //Pointer to your text field
...
NSFont *f = [NSFont fontWithName:@"Arial" size:12];
[textField setFont:f];

In answer to your second question, I think that this may point you in the right direction - however you will have to adapt the code to apply padding on the left-hand side of the text field rather than the right.

Perspx
Where would i enter this code, should I put it into a new class file and link it up to the text field?
Joshua
If the text field is created in a NIB/XIB file, then I would create a controller class for your application (unless one already exists) and implement it in the awakeFromNib function for your class (apologies, I meant to refer to the textField pointer as an outlet, perhaps I should have made that more clear); if you're creating the text field programatically then you can set the font upon creation. Of course if you are going to subclass NSTextFieldCell as shown in the link I posted then the methods for that go in your subclass's implementation file.
Perspx
I see, so is as simple as just copying and pasting that code above into the controller
Joshua
I meant to say, so is it as simple as just copying and pasting that code above into the controller?
Joshua
Yes, to solve your question regarding setting the text field's font.
Perspx
Just experimented and I made it work thanks to your code. Do you know where I should paste the code from the topic about the padding? Does it need to be in another controller?
Joshua
As the user said, you need to create a class which is a subclass of NSTextFieldCell (which controls the user interface of the text field), which is where you implement the custom behaviour of your text field that you require; take a look at the PhotoSearch example code that is posted, and also look at http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/ControlCell/ControlCell.html
Perspx