I created a nib file and want to display dynamic text messages on it like file names that are selected or the no of files selected etc. Is there a way to to this?
I know this can be done for alert panels but i want it on my custom sheets.
Thanks
Either create connections between your NSTextField elements and your controller class and then set the labels programmatically (using setStringValue).
Or you could consider using bindings. See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html.
You can create a NSTextField programmatically like this:
(IBAction)showText:(id)sender {
NSRect frame = NSMakeRect(50, 50, 200, 100);
NSTextField *tf = [[NSTextField alloc] initWithFrame:frame];
[tf setStringValue:#"test"];
[tf setSelectable:NO];
[tf setEditable:NO];
[tf setBordered:NO];
[tf setDrawsBackground:NO];
[[[sender window] contentView] addSubview:tf];
[tf release];
}
or you could use NSString's methods for drawing text in a view, namely -drawAtPoint or -drawInRect
Related
Let me elaborate the steps i am doing
Create Mac 10.9 Project
Drag a CustomView (Let's call it myView) to the .xib
Drag a NSButton to the .xib but out side the CustomView
Now programatically (using a other class) i add a NSTextField to the CustomView when the button is clicked by this code
NSTextField *textField = [[NSTextField alloc] init];
[textField setBezeled:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setFont:[NSFont fontWithName:#"Arial" size:20]];
[textField setStringValue:#"Hello World!"];
int textWidth = textField.fittingSize.width;
int textHeight = textField.fittingSize.height;
[textField setFrame:NSMakeRect(0, 0, textWidth, textHeight)];
[myView addSubview:textField];
Now i see that the TestField is added to myView
All i want is the user can drag the textField movable inside myView
i added the following code
- (void)mouseDown:(NSEvent *)event{
NSLog(#"Hi");
}
But the NSLog is not getting shown
How do i make the NSTextField draggable?
Note: Because mouse-moved events occur so frequently that they can
quickly flood the event-dispatch machinery, an NSWindow object by
default does not receive them from the global NSApplication object.
However, you can specifically request these events by sending the
NSWindow object an setAcceptsMouseMovedEvents: message with an
argument of YES.
From Apple Docset
I am using NSSplitview of which upper half is NSTableView and lower is NSTabview with 2 items. Each NStabViewItem has a NSTextview. All are defined in nib file.
Now on selection of row of NSTableview, I load content of file and set it on NSTextview of NSTabViewItem. so NSTabview items I load depending on what rows of NSTableView is selected.
However I observe appreciable delay of 5-6 seconds to see the text visible on NSTextview of NSTabViewItem of NSTabView.But if I hover the mouse in the NSTabView region the content of NSTextview of NSTabViewItem of NSTabview is shown immediately.
Can anybody guide me what is the issue?
I am only using NSTableView delegate, Not tabview delegate. I just load the content in respective NSTextView of each NSTabViewItem of tabview.
Code Snippet:
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
if ([mTableView selectedRow] != -1)
{
selectedNode = [mLogContainer objectAtIndex:[mTableView selectedRow]];
[self manageTabView:[selectedNode logfile]];
}
}
-(void) manageTabView:(NSString*) fname
{
[self loadTextView:mDetailView withFilePath:fname];
NSString* summaryFile = [NSString stringWithFormat:#"%#.summary",fname];
[self loadTextView:mSummaryView withFilePath:summaryFile];
[mTabView selectTabViewItemAtIndex:0];
}
-(void) setContent:(NSString*) content forView:(NSTextView*) textView
{
NSString* fileContent = [[NSString alloc] init];
[fileContent stringWithContentsOfFile:content
encoding:NSUTF8StringEncoding error:nil];
NSTextStorage *textStorage = [textView textStorage];
[textStorage beginEditing];
[[textStorage mutableString] setString:fileContent];
[textStorage endEditing];
[textView setNeedsDisplay:YES];
}
How much ever stupid it may sound, but the problem of this weird behavior was that the NStextview in the nib frame was a bit bigger than the NSTabviewItem view. Not sure what is the reason for this behavior but when I re-sized the NSTextView .. Everything is working fine.
I have a fixed number of text items I want to display with checkboxes to select/deselect in a cocoa tableview. Exactly like a listbox of items with checkboxes in Microsoft MFC.
If I drop the section labels and checkboxes into the TableView they don't seem to belong to the tableview. Note: the labels are to divide/designate the sections for the checkbox items.
What are the correct steps?
Thanks.
You can make a CustomCell class: take a NSTextField and NSButton(for checkbox)-
set UIButton state in IB:
for default state - set non selected image.
for selected state - set selected image.
In CustomCell.m file
-(void)setSelected:(BOOL)selected
{
[checkBoxBTN setSelected:selected];
}
you can do this in also cellForRowAtIndex method in your tableView class.
your table data source should be a ModelClass or a dictionary with two keys like title and its state(selected/nonSelected).
In didSelectAtIndexPath method you need to update your dictionary like:
NSMutableDictionary * dic = [yourDataSourceArray objectAtIndex:indexPath.row];
NSNumber *state = [dic valueForKey:#"state"];
[dic setValue:![state boolValue]];
You need to modify your cellForRowAtIndexPath Method to show prev selected/nonSelected state
NSMutableDictionary * dic = [yourDataSourceArray objectAtIndex:indexPath.row];
NSNumber *state = [dic valueForKey:#"state"];
cell.title = [dic valueForKey:#"title"];
[cell.checkBoxBtn setSelected:[state boolValue]];
Edit
To create Label:
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 200, 17)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[cell addSubview:textField];
I know how to do this in iOS but cannot figure it how to it in Cocoa.
I want to capture keyboard events and I think I need to override the acceptsFirstResponder method to accomplish that (keyDown method being triggered). So I created a class extending NSCustomView and tried to add it in the main Window but I just cannot understand how to do it. So far I added a Custom View to the main View then tried to add it programmatically like:
TestView *view = [[TestView alloc] init];
[[_window contentView] addSubview:view];
but this is not working. So how can I do this?
To see if the view has been added to a window, you can override the view's viewDidMoveToWindow method and log the value of [self window] to check (if it's nil then the view has been removed from a window):
- (void)viewDidMoveToWindow
{
NSLog(#"window=%p", [self window]);
[super viewDidMoveToWindow];
}
You should be subclassing NSView, not NSCustomView, and initWithFrame is the designated initializer for NSView, not init.
Try:
TestView *view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[[_window contentView] addSubview:view];
I want to create NSTextField programmatically. I am new to Mac App Development.
Can anyone help me in this ?
Update :
I tried this code
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *myView = [[NSView alloc] init];
NSRect frameRect = NSMakeRect(20,20,100,140);
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
}
This does nothing. Please correct me if i'm wrong anywhere.
Thank you.
Creating UI elements programmatically is very simple in Cocoa.
It involves two steps:
Create the view and set a frame to it.
Add it as a subview to any super view.
Following snippet will you help you understand better.
NSRect frameRect = NSMakeRect(20,20,40,40); // This will change based on the size you need
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
It looks like you're creating an NSTextField OK, but you're not adding it to anywhere visible in the view hierarchy. Your app probably contains one or more NSWindow instances; if you want a view to appear in a particular window, you should add it as a subview of that window's content view.