NSTextField Color issues - objective-c

I am dynamically adding a NSTextField to a window and I am having issues with rendering. I am setting the background color to be black and the text color to be white. These both work but their is what appears to be a rectangle that is part of the text that is always white. Does anyone know what I might be doing wrong? How can I get rid of the white background that is just around the text? Code is as follows:
//Create rectangle to size text field
NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);
//Instantiate text field and set defaults
NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];
[textField setFont:[NSFont fontWithName:#"Arial" size:48]];
[textField setTextColor:[NSColor whiteColor]];
[textField setStringValue:#"Some Text"];
[textField setBackgroundColor:[NSColor blackColor]];
[textField setDrawsBackground:YES];
[textField setBordered:NO];
[[window contentView] addSubview:textField];

I tried your code on Mac OS X 10.6.4.
Inside the application delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);
NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];
[textField setFont:[NSFont fontWithName:#"Arial" size:48]];
[textField setTextColor:[NSColor whiteColor]];
[textField setStringValue:#"Some Text"];
[textField setBackgroundColor:[NSColor blackColor]];
[textField setDrawsBackground:YES];
[textField setBordered:NO];
[[window contentView] addSubview:textField];
}
And this is the result
alt text http://www.freeimagehosting.net/uploads/26c04b6b64.png
I can't see any white box.
Maybe you are using a different OS.
Or maybe you have some other views on top of each other that are causing the weird effect you are talking about.

Try setting refusesFirstResponder = TRUE property of your NSTextField object. I have come across behavior you described in 10.7, in 10.6 everything works as expected.

Ok,
The mystery is partially solved. In conjunction with my NSTextField, I am also setting some NSApplicationPresentationOptions to put the application into Kiosk mode. It appears that something with that is causing the problem I am seeing. If I do not set the PresentationOptions the NSTextField displays exactly the way I want it to. I will track down what specific PresentationOption is to blame and post here.

Related

NSAlert not working with NSTextField

I have an NSAlert with a NSView accessory view that contains two NSTextField's. I can put the cursor in the NSTextField's but I cannot type in them. Instead, it will type in the last line I was typing on in Xcode. I am using Xcode 6.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSAlert *passRequest = [[NSAlert alloc] init];
[passRequest setMessageText:#"Finder wants to restart. Type your password to allow this."];
[passRequest addButtonWithTitle:#"OK"];
[passRequest addButtonWithTitle:#"Cancel"];
[passRequest setAccessoryView:[InputView inputViewWithUsername:#"James Pickering"]];
NSImage *lockImage = [[NSImage alloc] initWithContentsOfFile:#"LOCK_YOSEMITE.png"];
[passRequest setIcon:lockImage];
[passRequest runModal];
}
I did implement the LSUIElement key, but even before that it wasn't running properly. Otherwise, it's straight out of the box cocoa app.
Here is my code for InputView:
#import "InputView.h"
#interface InputView ()
#property (strong, nonatomic) NSString *username;
#end
#implementation InputView
+ (InputView *)inputViewWithUsername:(NSString *)username {
InputView *view = [[self alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 321, 52))];
[view setUsername:username];
return view;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSTextField *usernameLabel = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 32, 71, 17))];
[usernameLabel setStringValue:#"Username:"];
[[usernameLabel cell] setBordered:NO];
[[usernameLabel cell] setBezeled:NO];
[usernameLabel setEditable:NO];
[usernameLabel setSelectable:NO];
[usernameLabel setBackgroundColor:[NSColor clearColor]];
[usernameLabel setFont:[NSFont systemFontOfSize:13]];
[self addSubview:usernameLabel];
NSTextField *passwordLabel = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(2, 2, 69, 17))];
[passwordLabel setStringValue:#"Password:"];
[[passwordLabel cell] setBordered:NO];
[[passwordLabel cell] setBezeled:NO];
[passwordLabel setEditable:NO];
[passwordLabel setSelectable:NO];
[passwordLabel setBackgroundColor:[NSColor clearColor]];
[passwordLabel setFont:[NSFont systemFontOfSize:13]];
[self addSubview:passwordLabel];
NSTextField *usernameInput = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(77, 30, 206, 22))];
[usernameInput setStringValue:self.username];
[usernameInput setFont:[NSFont systemFontOfSize:13]];
[self addSubview:usernameInput];
NSTextField *passwordInput = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(77, 0, 206, 22))];
[passwordInput setFont:[NSFont systemFontOfSize:13]];
[self addSubview:passwordInput];
}
#end
I am calling this function in int main.
This is your problem. An alert needs a full application set up and running its event loop. In order for typing to go to it, it will need to be the active application.
You should probably create a normal app from Xcode's template. Start with that. Get things working there. You can present your alert in an action method that's connected to a menu item or a button in a window or whatever. (You could also present in the -applicationDidFinishLaunching: app delegate method, I suppose.)
After you've got that working, if there's some reason you need this to work in an unusual context (e.g. command-line tool), you can work on that.
You are modifying the view hierarchy inside of your -drawRect: method. You must not do this.
First, -drawRect: may be called many times over the life of the view and you would be adding subviews every time it draws. More and more subviews, over and over.
Second, even if you were careful to only add them the first time, -drawRect: is for drawing and not for changing the view hierarchy. (There's -viewWillDraw if you need to make such changes just before drawing, but I don't think that's appropriate for this case, either.)
You can add the subviews in an override of -initWithFrame: instead.
By the way, you should probably use NSMakeRect() instead of NSRectFromCGRect(CGRectMake(...)).

Why NSTextField only shows the first word

When I include an NSTextField to a view, the added data is not completely shown. When the same configuration is applied to an NSTextField added with the .xib the text is shown completely.
Here is the code:
// Generated programmatically
NSTextField *label = [[NSTextField alloc] initWithFrame:self.bounds];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
label.alignment = NSCenterTextAlignment;
label.backgroundColor = [NSColor clearColor];
label.textColor = [NSColor redColor];
label.font = [NSFont boldSystemFontOfSize:16.f];
[label setStringValue:str];
[self addSubview:label];
NSSize s3 = [str sizeWithAttributes:#{NSFontAttributeName:label.font}];
NSLog(#"S3 size: %f", s3.width);
[label setFrameSize:s3];
And the result is this:
Black text is the label configured in xib file. White text is the label created in xib file and configured programmatically. Red text is the label created and configured programmatically
Any idea?
The code is available here
Using NSString's sizeWithAttributes: only calculates the size of the text string itself. An NSTextField and its NSTextFieldCell also require some room themselves to handle their part of the drawing. So when you set the cell size to the size of the text alone, it's too small for the cell to draw all of the text and so the text is truncated.
Instead of trying to measure the size of the text yourself, you might try:
[label setStringValue:str];
[label sizeToFit];
[label setNeedsDisplay:YES];
NSTextField inherits NSControl's sizeToFit method which will automatically resize the text field to the appropriate size.

UITextField at the bottom of screen is unseen

Using UITextField that is in the bottom of screen, when the keyboard is opened, user cant see what is he writes because the keyboard covers the textfield.
Is there any way to see what you write or push up the text field page ?
userName = [[UITextField alloc] initWithFrame:CGRectMake(133, 280, 200, 30)];
[userName setDelegate:self];
[userName setText:#"NickName"];
[userName setBorderStyle:UITextBorderStyleRoundedRect];
[userName setTextColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[[[[CCDirector sharedDirector] view] window] addSubview:userName];
I think ..YOu have to use that textfield with Scrollview. This way the user can scroll up and so can see what he is typing.
Update: I found THIS link, which satisfy your need.

NSTextField doesn't show when I set AttributedString placeholder

I'm trying to customize a little bit my NSTextFields and the first step is to customize the placeholder.
I want to change the placeholder color, and I'm trying it out by this way:
- (void)awakeFromNib {
// Color for placeholder in NSTextField - Color: #cdc9c1
NSColor *placeholderColor = [NSColor colorWithCalibratedRed:0.80f green:0.78f blue:0.75f alpha:1.0f];
NSDictionary *placeholderAttributeDict = [NSDictionary dictionaryWithObject:placeholderColor forKey:NSForegroundColorAttributeName];
NSAttributedString *emailPlaceholderString = [[NSAttributedString alloc] initWithString:#"Email" attributes:placeholderAttributeDict];
// NSAttributedString *passPlaceholderString = [[NSAttributedString alloc] initWithString:#"Password" attributes:placeholderAttributeDict];
// NSTextField Email attributes
[[self emailTextField] setDrawsBackground:NO];
[[self emailTextField] setBordered:NO];
[[[self emailTextField] cell] setPlaceholderAttributedString:emailPlaceholderString];
// NSTextField Password attributes
[[self passTextField] setDrawsBackground:NO];
[[self passTextField] setBordered:NO];
[[[self emailTextField] cell] setPlaceholderString:#"Password"];
}
As you may see, the first placeholder in the first NSTextField is established by an NSAttributedString where i try to specify color. The second placeholder in the second NSTextField is just a NSString.
When i run the application, it only shows the second NSTextField. First one doesn't appear anywhere.
What is happening?
Thanks in advanced.
You are setting the same emailTextField twice...
[[[self emailTextField] cell] setPlaceholderAttributedString:emailPlaceholderString];
[[[self emailTextField] cell] setPlaceholderString:#"Password"];
(Does that fix things or was it just an error in the question?)

UITextView doesn't disappear when removedFromSuperview

I have two UITextViews, self.instructions and self.textView, that are supposed to alternate depending on what the user selects.
I create a self.textView like so:
-(void)createSpaceToWrite
{
[self.instructions removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO]; //This adds a UINavigationBar to the view.
if (!self.textView)
{
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 280, 150)];
}
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Helvetica Neue" size:14];
self.textView.text = #"";
[self.view addSubview:self.textView];
self.textView.delegate = self;
}
Then I create self.instructions like so:
-(void)haikuInstructions
{
[self.textView removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO];
if (!self.instructions)
{
self.instructions = [[UITextView alloc] initWithFrame:CGRectMake(10, 125, [[UIScreen mainScreen] bounds].size.width - 10, [[UIScreen mainScreen] bounds].size.height)];
}
self.instructions.backgroundColor=[UIColor clearColor];
self.instructions.text = #"Text of instructions";
self.instructions.editable=NO;
[self.view addSubview:self.instructions];
[self resignFirstResponder];
}
The user starts with self.instructions displayed against the background image. Fine.
The user switches. The instruction text disappears, to be replaced by the editable self.textView, a white box. Fine.
The user switches back. The instruction text appears--but the white box is still there, even thought I've removed it from the superview. And not only that, it's still editable and still brings up the keyboard when the user goes to edit it!
What am I doing wrong?
EDIT: Well, I basically scrapped all the code and started the class over from scratch, trying to be cleaner about everything, and I'm no longer having this problem, so it must have been something in some other method that was affecting it. Lesson: haphazard coding is bad!
Why do you need to remove your text views interchangeably? Wouldn't it be better to just "hide" them interchangeably by setting the setHidden property like for example:
[self.textView setHidden: YES];
and additionally try the following also:
[self.textView resignFirstResponder];
[self.textView setEditable: NO];
[self.textView setBackgroundColor: [UIColor clearColor]];
[self.textView setAlpha: 0.0];